SNipedr 2015-09-08 08:59 采纳率: 0%
浏览 2086
已结题

EF MVC 关联实体 更新操作 代码为什么要这么写?

这是从 EF自动生成的edit上修改的.关于文字类型的操作
ArticleCategory:

public partial class ArticleCategory : Entity
    {
        public ArticleCategory()
        {
            this.Article = new HashSet<Article>();
        }

        [Display(Name = "分类名称")]
        [StringLength(225, ErrorMessage = "内容太长", MinimumLength = 1)]
        public string Name { get; set; }

        public virtual ArticleCategory PId { get; set; }

        public virtual ICollection<Article> Article { get; set; }

        public virtual ICollection<ArticleCategory> Children { get; set; }
    }

在修改的过程中,发现如下写法,没有办法修改 PId

         public async Task Edit(ArticleCategory articleCategory,string pId)
        {
            if (pId!=null && pId!="0")
            {
                articleCategory.PId = await FindAsync(int.Parse(pId)); 
            }
            //ArticleCategory art = articleCategory;
            //articleCategory = await FindAsync(art.Id);
            //articleCategory.PId = art.PId;
            //articleCategory.Name = art.Name;
            //articleCategory.IsDelete = art.IsDelete;
            //articleCategory.OrderNo = art.OrderNo;
            //articleCategory.Remarks = art.Remarks;
            //articleCategory.UpdateTime = art.UpdateTime;

            db.Entry(articleCategory).State = EntityState.Modified;
            await SaveChangesAsync();
        }

写成这样会报错:

         public async Task Edit(ArticleCategory articleCategory,string pId)
        {

            if (pId!=null && pId!="0")
            {
                articleCategory.PId = await FindAsync(int.Parse(pId)); 
            }
            ArticleCategory art = articleCategory;
            articleCategory = await FindAsync(art.Id);
            articleCategory = art;
            //articleCategory.PId = art.PId;
            //articleCategory.Name = art.Name;
            //articleCategory.IsDelete = art.IsDelete;
            //articleCategory.OrderNo = art.OrderNo;
            //articleCategory.Remarks = art.Remarks;
            //articleCategory.UpdateTime = art.UpdateTime;

            db.Entry(articleCategory).State = EntityState.Modified;
            await SaveChangesAsync();
        }

Attaching an entity of type 'DataModel.ArticleCategory' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.

只有这样写了才行.

         public async Task Edit(ArticleCategory articleCategory,string pId)
        {
            if (pId!=null && pId!="0")
            {
                articleCategory.PId = await FindAsync(int.Parse(pId)); 
            }
            ArticleCategory art = articleCategory;
            articleCategory = await FindAsync(art.Id);
            articleCategory.PId = art.PId;
            articleCategory.Name = art.Name;
            articleCategory.IsDelete = art.IsDelete;
            articleCategory.OrderNo = art.OrderNo;
            articleCategory.Remarks = art.Remarks;
            articleCategory.UpdateTime = art.UpdateTime;

            db.Entry(articleCategory).State = EntityState.Modified;
            await SaveChangesAsync();
        }

已傻,求解答~~~~

  • 写回答

1条回答 默认 最新

  • threenewbee 2015-09-08 09:01
    关注

    public virtual ArticleCategory PId { get; set; }
    没必要定义virtual

    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试