开发者

How to delete data with linq to entities

This code is giving me this error:

The object cannot be deleted because it was not found in the ObjectStateManager

using (var context = new M开发者_开发百科vcApplication4.Entity.test2Entities())
            {

                var q = (from t in context.tag
                        where t.tag_id == tag
                        select new
                        {
                            t
                        }).FirstOrDefault();



                if (q != null)
                {
                    context.DeleteObject(q);
                    context.SaveChanges();
                }
            }

Am I missing something here?


You are creating an anonymous object with new {t} that just happens to contain a property called t of type Tag, which is not registered in the ObjectContext. You don't have to write select new {t}, just select t will return your Tag object.


Try just selecting t and not making an anonymous object with t as a property.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜