c# - Adding new entry to DataContext db Cause null reference -
i'm bit new datacontext should still simple.
i developing bidding site there many "products" each products many "bids".
 have 2 tables (persisted in .net datacontext model):
 bidproduct (with auctionid primary key)
 bidentry (with auctionid foreign key)
 relationship: bidproduct --> bidentry   
i trying add 1 product 1 linked bidentry db such: (list<products> , list<bidentries> pre-populated)
        dbdatacontext db = new dbdatacontext();         bidproduct p = products.first();         bidentry b = bids.where(bi => bi.auctionid == p.auctionid).first();          p.bidentries.add(b);          db.bidproducts.insertonsubmit(p);         db.submitchanges();  when p.bidentries.add(b) called null reference on bidentries!? if try a = new on bidentries still null reference.
what this!?
have initialized p.bidentries list<bidentries>?
the error suggests isn't.
you need ensure after point in code:
 bidproduct p = products.first(); p.bidentries not null - either assign new list<bidentry>() or ensure each product has non null bidentries property.
Comments
Post a Comment