Asp.net mvc Linq到Sql:InsertOnSubmit上出错 private Table galleryTable; 公共厨房存储库(字符串连接字符串){ dc=新数据上下文(connectionString); galleryTable=dc.GetTable(); } 公共图书馆(画廊){ 如果(gallery.GalleryId==0) 画廊桌。InsertOnSubmit(画廊); else if(galleryTable.GetOriginalEntityState(gallery)==null){ 茶几。附(茶几); galleryTable.Context.Refresh(RefreshMode.KeepCurrentValues,gallery); } galleryTable.Context.SubmitChanges(); }

Asp.net mvc Linq到Sql:InsertOnSubmit上出错 private Table galleryTable; 公共厨房存储库(字符串连接字符串){ dc=新数据上下文(connectionString); galleryTable=dc.GetTable(); } 公共图书馆(画廊){ 如果(gallery.GalleryId==0) 画廊桌。InsertOnSubmit(画廊); else if(galleryTable.GetOriginalEntityState(gallery)==null){ 茶几。附(茶几); galleryTable.Context.Refresh(RefreshMode.KeepCurrentValues,gallery); } galleryTable.Context.SubmitChanges(); },asp.net-mvc,linq-to-sql,insertonsubmit,Asp.net Mvc,Linq To Sql,Insertonsubmit,将新库插入表时,该方法会抛出一个未设置为对象错误实例的对象引用。画廊不为空,galleryTable也不为空 提前感谢所以问题出在我的画廊实体上 我有 private EntitySet\u标签; [System.Data.Linq.Mapping.Association(Storage=“\u Tags”,OtherKey=“TagId”)] 公共实体集标记 { 获取{返回此。_Tags;} 设置{this.\u标记。Assign(value);} } 它在this.\u Tags.Assi

将新库插入表时,该方法会抛出一个未设置为对象错误实例的对象引用。画廊不为空,galleryTable也不为空
提前感谢

所以问题出在我的画廊实体上 我有

private EntitySet\u标签;
[System.Data.Linq.Mapping.Association(Storage=“\u Tags”,OtherKey=“TagId”)]
公共实体集标记
{
获取{返回此。_Tags;}
设置{this.\u标记。Assign(value);}
}
它在
this.\u Tags.Assign(value)上抛出一个空引用
所以我给_Tags变量分配了一个空白的EntitySet,问题就解决了

   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();
    }
   private EntitySet<Tag> _Tags;

    [System.Data.Linq.Mapping.Association(Storage = "_Tags", OtherKey = "TagId")]
    public EntitySet<Tag> Tags
    {
        get { return this._Tags; }
        set { this._Tags.Assign(value); }
    }