我有一个拥有数百个属性的实体。虽然它在数据库中很好,但在课堂上它很不方便。问题是,我如何将类的一些属性组合到其他类中,这样在编程时会更方便,同时只保留一个表。
伪代码示例:
class Work {
WorkVolumes WorkVolumes;
...
SomeOtherGroupOfProperties SomeOtherGroup;
}
class WorkVolumes {
float VolumeOfWorkType1;
float VolumeOfWorkType2;
...
float VolumeEtc;
}
class SomeOtherGroupOfProperties {
int SomeOtherProperty;
...
}
在数据库中,只有表可以使用列VolumeOfWorkType1,VolumeOfWorkType2,VolumeEtc,SomeOtherProperty,...
答案 0 :(得分:1)
请参阅此处的“自有实体类型的自动表拆分”: https://blogs.msdn.microsoft.com/dotnet/2017/06/28/announcing-ef-core-2-0-preview-2/
对于以下模型,只创建一个表:
modelBuilder.Entity<Order>().OwnsOne( p => p.OrderDetails, cb => { cb.OwnsOne(c => c.BillingAddress); cb.OwnsOne(c => c.ShippingAddress); }); public class Order { public int Id { get; set; } public OrderDetails OrderDetails { get; set; } } public class OrderDetails { public StreetAddress BillingAddress { get; set; } public StreetAddress ShippingAddress { get; set; } } public class StreetAddress { public string Street { get; set; } public string City { get; set; } }