I am using EF5 with SQL Server 2012 in a web application. I have two classes:
public partial class Topic {
public Topic()
{
this.SubTopics = new List<SubTopic>();
}
public int TopicId { get; set; }
public string Name { get; set; }
public int Number { get; set; }
public virtual ICollection<SubTopic> SubTopics { get; set; }
}
public partial class SubTopic
{
public int SubTopicId { get; set; }
public int TopicId { get; set; }
public string Name { get; set; }
public string Notes { get; set; }
public virtual byte[] Version { get; set; }
public virtual Topic Topic { get; set; }
}
In our front-end we make a change to SubTopic Notes and then when a Save button is pressed the Topic object together with its SubTopic Objects are sent back as JSON to the Web API Controller. When we check what is being sent back we see the new data for the Notes. When we check the parameter topic we also see the new Notes data.
public HttpResponseMessage PutTopic(int id, Topic topic)
{
_uow.Topics.Update(topic);
_uow.Commit();
}
However checking with SQL Profiler we cannot see anything happening to change the Sub Topic. When data is retrieved the old SubTopic data is returned and the edit to notes is lost.
For the case of a Web update like this. How does EF determine what has changed and is there some way we can make it check / compare what's there with the new object so that it can detect a change and also update the Subtopic ?
Configuration:
DbContext.Configuration.ProxyCreationEnabled = false;
DbContext.Configuration.LazyLoadingEnabled = false;
DbContext.Configuration.ValidateOnSaveEnabled = false;
DbContext.Configuration.ValidateOnSaveEnabled = true;
DbContext.Configuration.AutoDetectChangesEnabled = false;
Update Method:
public virtual void Update(T entity)
{
DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
if (dbEntityEntry.State == EntityState.Detached)
{
DbSet.Attach(entity);
}
dbEntityEntry.State = EntityState.Modified;
}