Linq to Sql:InsertOnSubmit 时出错
private Table<Gallery> galleryTable;
public GalleryRepository ( string connectionString ) {
dc = new DataContext(connectionString);
galleryTable = dc.GetTable<Gallery>();
}
public void SaveGallery(Gallery gallery) {
if (gallery.GalleryId == 0)
galleryTable.InsertOnSubmit(gallery);
else if (galleryTable.GetOriginalEntityState(gallery) == null) {
galleryTable.Attach(gallery);
galleryTable.Context.Refresh(RefreshMode.KeepCurrentValues, gallery);
}
galleryTable.Context.SubmitChanges();
}
将新图库插入表中时,该方法会抛出“对象引用未设置为对象实例”错误。 gallery 不为 null,galleryTable 也不为 null 提前致谢
private Table<Gallery> galleryTable;
public GalleryRepository ( string connectionString ) {
dc = new DataContext(connectionString);
galleryTable = dc.GetTable<Gallery>();
}
public void SaveGallery(Gallery gallery) {
if (gallery.GalleryId == 0)
galleryTable.InsertOnSubmit(gallery);
else if (galleryTable.GetOriginalEntityState(gallery) == null) {
galleryTable.Attach(gallery);
galleryTable.Context.Refresh(RefreshMode.KeepCurrentValues, gallery);
}
galleryTable.Context.SubmitChanges();
}
When inserting a new gallery into the table, the method throws a Object reference not set to an instance of an object error. The gallery is not null and neither is the galleryTable
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以问题出在我的画廊实体上
我有
,它在
this._Tags.Assign(value);
处抛出一个空引用所以我为 _Tags 变量分配了一个空白的 EntitySet 并解决了问题
So the problem was with my Gallery entity
I had
and it was throwing a null reference at
this._Tags.Assign(value);
So i assigned a blank EntitySet to the _Tags variable and problem solved