Group properties into classes and map them to the same table as root class in Entity Framework Core

asp.net-core c# ef-core-2.0 entity-framework entity-framework-core

Question

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, ...

1
1
10/10/2017 10:50:15 AM

Accepted Answer

see "Automatic table splitting for owned entity types" here: 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; }
}
1
10/10/2017 11:41:07 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow