Linq to Sql:InsertOnSubmit出错

时间:2011-02-24 06:16:38

标签: asp.net-mvc 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();
    }

将新图库插入表中时,该方法会抛出未将对象引用设置为对象错误的实例。该库不是null,也不是galleryTable 提前致谢

1 个答案:

答案 0 :(得分:0)

问题出在我的Gallery实体上 我有

   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并解决了问题