0

运行代码时,我在插入时收到以下错误

An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext.  This is not supported.

我的代码

using (PostDBDataContext dataContext = new PostDBDataContext())
{
    if (posttypeedit.posttypemeta != null)
    {
        foreach (var posttypemeta in posttypeedit.posttypemeta)
        {
            PostTypeMeta _postmeta = new PostTypeMeta();
            _postmeta.vcr_MetaKey = posttypemeta.Metatexts;
            _postmeta.vcr_MetaValue = posttypemeta.Datatypes;
            _postmeta.int_ModifiedBy = Authorization.UserID;
            _postmeta.dtm_ModifiedDate = System.DateTime.Now;
            psttyp.PostTypeMetas.Add(_postmeta);
        }
    }
    dataContext.PostTypes.InsertOnSubmit(psttyp);                
    dataContext.SubmitChanges();  
}
4

1 回答 1

0

你正在插入psttyp

InsertOnSubmit(psttyp);    

但是 ptsstyp 不是新的,所以你不能再次插入它,

如果您的意图只是PostTYpeMeta 添加到您现有的 psttyp 中,您可以删除

dataContext.PostTypes.InsertOnSubmit(psttyp);    

完全地。

于 2012-12-20T16:18:32.833 回答