Search code examples
c#entity-frameworkef-code-firstentity-framework-6

Ignore one property of a complex type


I want to ignore one property of a complex type for mapping to the database where FinalTotal is a computed field. Entity Framework says that it isn't allowed and that it must be a property. :(

public class Sale
{
     public int      Id { get; set; }
     public DateTime DateSale { get; set; }
     public Amounts  Amounts { get; set; }
}

public class Amounts
{
     public decimal Subtotal { get; set; }
     public decimal Tax { get; set; }
     public decimal FinalTotal { get; set; }
}

public class SaleMap : EntityTypeConfiguration<Sale>
{
     public SaleMap()
     {
          Ignore(s => s.Amounts.FinalTotal);
     }
}

Solution

  • public class AmountsMap : EntityTypeConfiguration<Amounts>
    {
         public AmountsMap()
         {
              Ignore(a => a.FinalTotal);
         }
    }