I have a DbContext with ProxyCreationEnabled set to true (actually it's the default value).
As far as I remember, this enables EF to load proxy entities from database, so any change we make to properties are recognized by the change tracker, and we can call SaveChanges() like this:
using (var db = new MyDbContext())
{
var people = db.People.Where(p => p.Status = PersonStatus.New).ToList();
foreach (var person in people)
{
person.Name = "Something";
}
db.SaveChanges();
}
The problem is: why would EF not use the proxy for a specific class, even though ProxyCreationEnabled is true? The class is not sealed, so it should be able to use proxy.
Here is my sample class:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime RegisterDate { get; set; }
public PersonStatus Status { get; set; }
}
First()orToList()return the class instead of the proxy.AutoDetectChangesEnabledistrue, the lack of proxy should not be a problem at all.DbContext"tracks" the entity instances internally, including the original and current values.AutoMapperto update the values from one list of objects (e.g PersonViewModel) to the destination existing entities. I thought it was due to proxies, sinceAutoMapperkeeps the destination instances. I'm going to do more tests. Anyway, my question was properly answered by MegaTron, but you pointed something really interesting.