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'."
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.
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.
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.
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.
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; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With