I have a DB-first EF core model OrderableItem
with a helper property added in its partial part:
public partial class OrderableItem {
public decimal EffectivePrice {
get => IsSpecial ? Special.Price : Price;
set {
if (IsSpecial)
Special.Price = value;
else
Price = value;
}
}
}
however, after adding this property I can no longer query the DbSet (MySql provider), throwing an exception:
MySql.Data.MySqlClient.MySqlException (0x80004005): Unknown column 'i.EffectivePrice' in 'field list'
while the exception is clear, it does not make any sence why EF is trying to query a property that is not defined in model (and should not be!). How can I fix this issue? Am I limited to methods in partials?
You should specify properties which should be ignored by DbContext using modelBuilder's method ignore. builder.Entity<OrderableItem>().Ignore(x => x.EffectivePrice)
.
You can achieve same using attributes but if your class is used as domain entity then it should not contain any infrastructure dependencies.
By default, EF Core would try to map all the properties as long as they have both getter and setter. If you need to ignore a specific property, you can use NotMapped
attribute:
public partial class OrderableItem {
[NotMapped]
public decimal EffectivePrice {
get => IsSpecial ? Special.Price : Price;
set {
if (IsSpecial)
Special.Price = value;
else
Price = value;
}
}
}
User contributions licensed under CC BY-SA 3.0