c# - NullReferenceException with Auto-Generated Tables -
i have problem exception nullreferenceexception ... class auto generated ef , contains list of icollection , list supposed initialized in constructor when trying add items list shows exception.
internal partial class customer : person { partial void objectpropertychanged(string propertyname); public customer() { this.accounts = new hashset<account>(); this.customerupdates = new hashset<customerupdate>(); } public virtual icollection<account> accounts { get; set; } public virtual icollection<customerupdate> customerupdates { get; set; } }
the exception thrown when trying add item collection. "this.accounts.add()"
internal partial class customer : person, icustomer { internal customer(guid userid, string firstname, string surname) : base(userid, firstname, surname) { } //list of customer accounts ienumerable<iaccount> icustomer.accounts { { return accounts.asenumerable<iaccount>(); } } //open savingsaccount public account opensavingsaccount(decimal amount) { var account = new accountsavings(); account.debit(amount, "-- opening balance --"); this.accounts.add(account); return account; } //open loanaccount public account openloanaccount(decimal amount) { var account = new accountloan(amount); account.debit(amount, "-- opening balance --"); this.accounts.add(account); return account; }
entity framework initializes collections if use .include(o => o.accounts)
in query.
if not have include have initialize list yourself:
if (this.accounts == null) this.accounts = new list<account>(); this.accounts.add(account);
Comments
Post a Comment