2011-02-24 70 views
0
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(); 
    } 

向表中插入新的圖庫時,該方法將引發未設置爲對象錯誤實例的對象引用。這個畫廊是不是null,也不是事先Linq to Sql:InsertOnSubmit上的錯誤

回答

0

所以問題是與 我

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); } 
    } 

我的畫廊實體galleryTable 感謝,並將其在this._Tags.Assign(value); 拋出一個空引用所以我分配了一個空的EntitySet到_Tags變量並解決問題