Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Owned type mapping EF Core fails when saving

I want to make TableSplitting using Owned Types. I have the following model:

public class Account  {   public GUID Id { get; set; }   public string Email { get; set; }   public StreetAddress Address { get; set; } }  public class StreetAddress {   public string Name { get; set; }   public string Address { get; set; }   public string Zipcode { get; set; }   public string City { get; set; }   public string  Country { get; set; }   public Location Location { get; set; } }  public class Location {   public double Lat { get; set; }   public double Lng { get; set; } } 

And I defined my mapping of the Account like this:

public override void Map(EntityTypeBuilder<Account> map) {     // Keys     map.HasKey(x => x.Id);      // Indexs     map.HasIndex(x => x.Email).IsUnique();      // Property mappings.     map.Property(x => x.Email).HasMaxLength(255).IsRequired();      // Owned types.     map.OwnsOne(x => x.Address, cb => cb.OwnsOne(a => a.Location)); } 

When I run the migration things are working and the columns are created in the database. But when I try to insert and save an address like so:

var account1 = new Account("[email protected]", "First", "Last")     {       Address = new StreetAddress()                   {                         Address1 = "Street 1",                         City = "City",                         Zipcode = "2000",                         Country = "Denmark",                         Location = new Location()                         {                             Lat = 0.0,                             Lng = 5.5                         }                      }                 }; this.Context.Accounts.Add(account1); 

I get this error

Message "The entity of 'Account' is sharing the table 'Accounts' with 'Account.Address#StreetAddress', but there is no entity of this type with the same key value 'Id:b7662057-44c2-4f3f-2cf0-08d504db1849' that has been marked as 'Added'."

like image 791
Rune Jensen Avatar asked Sep 26 '17 08:09

Rune Jensen


People also ask

When should you not use Efcore?

One of the biggest reasons not to use Entity Framework Core is that your application needs the fastest possible data access. Some applications do a lot of heavy data operations with very high-performance demands, but usually business applications don't have that high of a performance demand.

What is owned entity EF core?

EF Core allows you to model entity types that can only ever appear on navigation properties of other entity types. These are called owned entity types. The entity containing an owned entity type is its owner.

Should I use EF6 or EF core?

Keep using EF6 if the data access code is stable and not likely to evolve or need new features. Port to EF Core if the data access code is evolving or if the app needs new features only available in EF Core. Porting to EF Core is also often done for performance.

Is EF core faster than EF6?

EF Core 6.0 performance is now 70% faster on the industry-standard TechEmpower Fortunes benchmark, compared to 5.0. This is the full-stack perf improvement, including improvements in the benchmark code, the . NET runtime, etc. EF Core 6.0 itself is 31% faster executing queries.


1 Answers

Consider using virtual properties. This can also be used for lazy loading in your application

 public class Account   {   public GUID Id { get; set; }   public string Email { get; set; }   public virtual StreetAddress Address { get; set; }  }  public class StreetAddress {   public string Name { get; set; }   public string Address { get; set; }   public string Zipcode { get; set; }   public string City { get; set; }   public string  Country { get; set; }   public virtual Location Location { get; set; } }  public class Location {   public double Lat { get; set; }   public double Lng { get; set; } } 
like image 69
dwakel Avatar answered Oct 03 '22 14:10

dwakel