C# EF只是第一次编辑我的实体
我有一个WCF Rest服务,如您所见:C# EF只是第一次编辑我的实体,c#,entity-framework,web-services,wcf,C#,Entity Framework,Web Services,Wcf,我有一个WCF Rest服务,如您所见: [OperationContract] [WebInvoke(Method = "PUT", UriTemplate = "/EditNews", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] bool Edit(News entity); News student = new News
[OperationContract]
[WebInvoke(Method = "PUT", UriTemplate = "/EditNews", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
bool Edit(News entity);
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());
使用此代码:
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;
}
}
}}
因此,我在我的客户机中调用我的服务来编辑我的实体,如您所见:
[OperationContract]
[WebInvoke(Method = "PUT", UriTemplate = "/EditNews", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
bool Edit(News entity);
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());
因此,我运行我的服务和客户端应用程序,点击编辑按钮,我的编辑工作。但这是我第二次在编辑方法中遇到这个错误
确保没有任何其他实体附加到您的新闻模型,可能它正试图添加一个附加到您的新闻实体的实体,只需传递它而不包含任何子对象,并确保在选择此实体时使用了AsNoTracking 最终解决方案
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;
}
}
没有。我只是对wcf REST发送的新闻有一个距离。我的意思是在编辑期间是否附加了任何导航属性,如果没有,请在get上选择时检查AsNoTracking。您可以详细告诉我如何使用AsNoTracking来避免此错误吗?在编辑对象之前,请调用数据库以获取该对象。那个调用,确保上次我遇到这个错误时,它变成了我忘记添加asnotracking它可能被缓存在您的上下文中,这就是为什么当您使用asnotracking时它不会被缓存,所以它会知道它是被修改的同一项