Map navigation property to main table

北城余情 提交于 2021-02-07 20:43:14

问题


I have class Contract with two properties TotalAmount and InstallmentAmount

public class Contract
{
    public int ContractId { get; set; }
    public Amount TotalAmount { get; set; }
    public Amount InstallmentAmount { get; set; }
    //other Amounts
}

public class Amount
{
    public decimal Value { get; set; }
    public string Currency { get; set; }
} 

Is it possible to configure Entity Framework so it can create one table Contract with structure like below:

------------------------------------------------------------
| ContractId | TotalAmountValue | TotalAmountCurrency | ... 
|     999    |       1000       |         USD         | ...
------------------------------------------------------------  

回答1:


Answering your concrete question. What you are asking is possible by mapping the Amount class as owned entity type.

The simplest way to do that is to use [Owned] attribute:

[Owned] // <--
public class Amount
{
    public decimal Value { get; set; }
    public string Currency { get; set; }
}

or fluent API:

modelBuilder.Owned<Amount>();

This by default will create a single table in question, but the column names will be TotalAmount_Value, TotalAmount_Currency etc. If that's ok, you are done.

If you want to remove the underscore in column names, you'd need to use OwnsOne fluent API for each Contract.Amount property and then Property(...).HasColumnName(...) for each Amount property. Instead of doing that manually, you could do that in a loop using the EF Core metadata services. For instance:

modelBuilder.Entity<Contract>(builder =>
{
    var amounts = builder.Metadata.GetNavigations()
        .Where(n => n.ClrType == typeof(Amount));
    foreach (var amount in amounts)
    {
        var amountBuilder = builder.OwnsOne(amount.ClrType, amount.Name);
        var amountProperties = amountBuilder.OwnedEntityType.GetProperties()
            .Where(p => !p.IsKey());
        foreach (var property in amountProperties)
            amountBuilder.Property(property.Name).HasColumnName(amount.Name + property.Name);
    }
});


来源:https://stackoverflow.com/questions/57093110/map-navigation-property-to-main-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!