将属性分组到 classes 并将它们映射到与 Entity Framework Core 中的根 class 相同的 table
Group properties into classes and map them to the same table as root class in Entity Framework Core
我有一个拥有数百个属性的实体。虽然它在数据库中很好,但在 classes 中却很不方便。问题是,如何将 class 的一些属性分组到其他 classes 中,这样在编程时会更方便,同时只保留一个 table.
伪代码示例:
class Work {
WorkVolumes WorkVolumes;
...
SomeOtherGroupOfProperties SomeOtherGroup;
}
class WorkVolumes {
float VolumeOfWorkType1;
float VolumeOfWorkType2;
...
float VolumeEtc;
}
class SomeOtherGroupOfProperties {
int SomeOtherProperty;
...
}
虽然在数据库中只有 table Work with columns VolumeOfWorkType1, VolumeOfWorkType2, VolumeEtc, SomeOtherProperty, ...
请在此处查看 "Automatic table splitting for owned entity types":
https://blogs.msdn.microsoft.com/dotnet/2017/06/28/announcing-ef-core-2-0-preview-2/
for the following model only one table is created:
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; }
}
我有一个拥有数百个属性的实体。虽然它在数据库中很好,但在 classes 中却很不方便。问题是,如何将 class 的一些属性分组到其他 classes 中,这样在编程时会更方便,同时只保留一个 table.
伪代码示例:
class Work {
WorkVolumes WorkVolumes;
...
SomeOtherGroupOfProperties SomeOtherGroup;
}
class WorkVolumes {
float VolumeOfWorkType1;
float VolumeOfWorkType2;
...
float VolumeEtc;
}
class SomeOtherGroupOfProperties {
int SomeOtherProperty;
...
}
虽然在数据库中只有 table Work with columns VolumeOfWorkType1, VolumeOfWorkType2, VolumeEtc, SomeOtherProperty, ...
请在此处查看 "Automatic table splitting for owned entity types": https://blogs.msdn.microsoft.com/dotnet/2017/06/28/announcing-ef-core-2-0-preview-2/
for the following model only one table is created:
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; } }