2011-02-24 98 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变量并解决问题