首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >EF刚刚第一次编辑了我的实体

EF刚刚第一次编辑了我的实体
EN

Stack Overflow用户
提问于 2017-05-29 04:42:31
回答 2查看 47关注 0票数 0

我有WCF-Rest服务,如您所见:

代码语言:javascript
复制
 [OperationContract]
        [WebInvoke(Method = "PUT", UriTemplate = "/EditNews", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        bool Edit(News entity);

使用此代码:

代码语言:javascript
复制
public class NewsRepository :INewsRepository
    {
        private readonly DataContext _ctx;
        public NewsRepository(DataContext ctx)
        {
            _ctx = ctx;

        }

        public bool Add(News entity)
        {
            try
            {
                _ctx.News.Add(entity);
                _ctx.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                // TODO log this error
                return false;
            }
        }

        public bool Edit(News entity)
        {
            try
            {

                _ctx.Entry(entity).State = System.Data.Entity.EntityState.Modified;
                _ctx.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                // TODO log this error
                return false;
            }
        }
}}

因此,我在我的客户端调用我的服务来编辑我的实体,如您所见:

代码语言:javascript
复制
 News student = new News
            {
                Id = Guid.Parse("7320D87D-4819-4663-BCF9-2D09F9E4BD70"),
                Subject = "aaaaaaaaaaaaaassssssssssss",
                ViewerCounter = 3, // removed the "" (string)
                MainContent = "fsdsd", // renamed from "Content"
                SubmitDateTime = DateTime.Now,
                ModifiedDateTime = DateTime.Now,
                PublisherName = "sdaadasd",
                PictureAddress = "adfafsd",
                TypeOfNews = "bbbbb"
            };
            WebClient Proxy1 = new WebClient();
            Proxy1.Headers["Content-type"] = "application/json";
            MemoryStream ms = new MemoryStream();
            DataContractJsonSerializer serializerToUplaod = new DataContractJsonSerializer(typeof(News));
            serializerToUplaod.WriteObject(ms, student);
            byte[] a = Proxy1.UploadData("http://localhost:47026/NewsRepository.svc/EditNews", "PUT", ms.ToArray());

所以我运行我的服务和我的客户端应用程序,第二次点击编辑按钮和编辑works.But,我在我的Edit method中得到了这个错误。

代码语言:javascript
复制
Attaching an entity of type 'CMSManagement.Domain.Entity.News' 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.
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-05-29 10:22:25

最终解

代码语言:javascript
复制
  public bool Edit(News entity)
            {
                try
                {
                    News Edited = _ctx.News.Where(i => i.Id == entity.Id).First();
                    _ctx.Entry(Edited).CurrentValues.SetValues(entity);
                    _ctx.SaveChanges();
                    return true;
                }
                catch (Exception ex)
                {
                    // TODO log this error
                    return false;
                }
            }
票数 0
EN

Stack Overflow用户

发布于 2017-05-29 05:15:37

确保没有任何其他实体附加到您的新闻模型,也许它是试图添加一个实体附加到您的新闻实体,只是传递它没有任何子对象,并确保您使用AsNoTracking()在选择该实体。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44234725

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档