entity framework - Using Many to many with ef linq expression gave me "Unable to create a constant value of type (type)" -
i have table, discounts relates customergroups , product.
keys:
- product has key int productid.
- discount has key int discountid.
- customergroup has key int customergroupid.
relations:
- the relation between discounts , customergroups one-to-many , nullable (a discount has nullable customergroup).
- the relation between discounts , products many-to-many , goes via connection table discount_products. table has composite key consisting of 2 ints discountid , productid. connection table automatically invisible in model diagram , relation both ends *.
i have variable discounts1 populate using ef:
iqueryable<models.discount> discounts1 = _entities.discounts;
what want ask discount percentages discounts not connected customer group , related product.
the linq expression try use is:
var candidates = (from discount in discounts1 (discount.customergroup == null) && discount.products.contains(product) select discount.percentage).tolist();
what when running code notsupportedexception message
unable create constant value of type 'models.product'. primitive types or enumeration types supported in context.
what doing wrong?
the entity framework can't translate contains(product)
sql code. problem lies here:
discount.products.contains(product)
you should search product it's productid
, should primitive type.
this know issue documented here: referencing non-scalar variables not supported :
referencing non-scalar variables, such entity, in query not supported. when such query executes, notsupportedexception exception thrown message states "unable create constant value of type entitytype. primitive types ('such int32, string, , guid') supported in context."
Comments
Post a Comment