1

I have an entity with hundreds of properties. While it is good in database, in classes it is inconvenient. The question is, how can I group some properties of the class into other classes so it would be more convenient in programming, while keeping only one table.

Pseudocode example:

class Work {
 WorkVolumes WorkVolumes;
 ...
 SomeOtherGroupOfProperties SomeOtherGroup;
}

class WorkVolumes {
 float VolumeOfWorkType1;
 float VolumeOfWorkType2;
 ...
 float VolumeEtc;
}

class SomeOtherGroupOfProperties {
 int SomeOtherProperty;
 ...
}

While in database there is only table Work with columns VolumeOfWorkType1, VolumeOfWorkType2, VolumeEtc, SomeOtherProperty, ...

4

1 回答 1

2

请参阅此处的“自有实体类型的自动表拆分”: 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; }
}
于 2017-10-10T11:33:31.037 回答