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

将新图库插入表中时,该方法会引发 Object reference not set to an instance of an object 错误。画廊不是空的,galleryTable 也不是提前谢谢

4

1 回答 1

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

它在抛出一个空引用this._Tags.Assign(value); 所以我为_Tags变量分配了一个空白EntitySet并解决了问题

于 2011-02-24T06:46:05.407 回答