EF core 'Unknown column in feld list' after adding property in partial class

0

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?

c#
mysql
entity-framework-core
asked on Stack Overflow May 11, 2020 by wondra

2 Answers

2

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.

answered on Stack Overflow May 11, 2020 by MistGun
1

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;
    }
  }
}
answered on Stack Overflow May 11, 2020 by Sergey Kudriavtsev

User contributions licensed under CC BY-SA 3.0