首页 > 其他分享 >EF Core HasMany vs OwnsMany

EF Core HasMany vs OwnsMany

时间:2022-11-15 10:57:14浏览次数:81  
标签:Core set get EF owned entity HasMany OwnsMany public

EF Core HasMany vs OwnsMany

回答1

From documentation:

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.

Owned entities are essentially a part of the owner and cannot exist without it, they are conceptually similar to aggregates.

https://learn.microsoft.com/en-us/ef/core/modeling/owned-entities

 

回答2

One of the differences is that relationships configured with OwnsMany() will include the owned entities by default when querying the owner from the database, whilst when using WithMany() you have to specify AutoInclude() manually if you want them to be included every time you get the owner entity form the database.

Also from documentation: Querying owned types

 

EF Core: Owned Entity Types

So here you should be able to see a very important thing. When we are getting User, we didn't have to explicitly Include Address to load Address details, it's loading its Owned Entities by default. This might not be a good example, since it's in the same table, we will see a clearer example when we are going through OwnsMany later in this post.    Note: I am using anotherContext here for retrieval, because EF Core will automatically fix-up navigation properties to any other entities that were previously loaded into the context instance. So even if you don't explicitly include the data for a navigation property, the property may still be populated if some or all of the related entities were previously loaded. Above was kind of a one-to-one relationship. Now let's have a look at one-to-many type of relationship.
public class User
{
    public int Id { get; set; }
 
    public string Name { get; set; }
 
    public ICollection<Address> Addresses { get; set; }
}
public class Address
{
    public string Street { get; set; }
 
    public string City { get; set; }
 
    public string Type { get; set; }
}
Now I can use OwnsMany as below.
public class UserEntityConfiguration : IEntityTypeConfiguration<User>
{
    public void Configure(EntityTypeBuilder<User> builder)
    {
        builder.ToTable($"{nameof(User)}");
 
        builder.OwnsMany(x => x.Addresses, y =>
        {
            y.ToTable("UserAddress");
 
            y.Property(y => y.City)
                .HasColumnName("City");
 
            y.Property(y => y.Street)
                .HasColumnName("Type");
            y.Property(y => y.Street)
                .HasColumnName("Type");
        });
    }
}

 

 

And here also, without doing .Include(x => x.Addresses), the addresses are being returned.   So that's about it. You can read more on Owned Entity Types by going to the below link.    Owned Entity Types   There are some restrictions when it comes to Owned Entities which makes perfect sense.
  • You cannot create a DbSet<T> for an owned type.
  • You cannot call Entity<T>() with an owned type on ModelBuilder.
  • Instances of owned entity types cannot be shared by multiple owners (this is a well-known scenario for value objects that cannot be implemented using owned entity types).
Hope this helps.   Happy Coding.   Regards, Jaliya      

 

 

 

 

 

 

 

 

 

标签:Core,set,get,EF,owned,entity,HasMany,OwnsMany,public
From: https://www.cnblogs.com/chucklu/p/16891647.html

相关文章