Ruslan_K
Ruslan_K

Reputation: 423

Map navigation property to main table

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

Upvotes: 3

Views: 257

Answers (1)

Ivan Stoev
Ivan Stoev

Reputation: 205629

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);
    }
});

Upvotes: 3

Related Questions