c# - NullReferenceException: Object reference not set to an instance of an object in code first entity framework class -
this question has answer here:
- what nullreferenceexception, , how fix it? 29 answers
public decimal total { { return quantity * pricelist.price; } set { total = value; } }
you need change logic:
public decimal total { { return quantity * (pricelist == null ? 0 : pricelist.price);} set { total = value; } }
total 0 if pricelist null. may want change logic, example, if pricelist null total should null too? changr total type decimal? , return null in case.
edit: assuming here price property "pricelist" non nullable. if nullable, take note of comments made below.
Comments
Post a Comment