向 DataContext db 添加新条目导致空引用
我对 DataContext 有点陌生,但它应该仍然很简单。
我正在开发一个投标网站,其中有许多“产品”,每个产品都有许多“投标”。
我有 2 个表(在 .NET 中保存到 DataContext 模型):
BIDPRODUCT(以auctionID为主键)
BIDENTRY(以auctionID作为外键)
关系:投标产品 --> BIDENTRY
我正在尝试将 1 个带有 1 个链接双叉的产品添加到数据库,如下所示: (List
和 List
已预先填充)
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();
当调用 p.BidEntries.Add(b)
时,我得到一个BidEntries
上的空引用!? 如果我在 BidEntries
上尝试 a = new
,我仍然得到空引用。
这是什么!?
I'm a bit new to DataContext but it should still be simple.
I am developing a bidding site where there are many "Products" with each products many "Bids".
I have 2 tables (persisted in .NET to a DataContext Model):
BIDPRODUCT (with auctionID as primary key)
BIDENTRY (with auctionID as foreign key)
RELATIONSHIP: BIDPRODUCT --> BIDENTRY
I am trying to add 1 product with 1 linked bidentry to the db as such:
(List<Products>
and List<BidEntries>
is 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)
is called I get a null reference on BidEntries
!?
if I try a = new
on BidEntries
I still get a null reference.
What is this!?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否已将
p.BidEntries
初始化为List
?该错误表明事实并非如此。
您需要确保代码中的这一点之后:
p.BidEntries
不为 null - 为其分配一个new List()
或确保每个产品具有非 nullBidEntries
属性。Have you initialized
p.BidEntries
to aList<BidEntries>
?The error suggests that it isn't.
You need to ensure that after this point in your code:
p.BidEntries
is not null - either assign anew List<BidEntry>()
to it or ensure that each product has a non nullBidEntries
property.