I'm trying to learn more about Entity Framework 5 and the DbContext
, and I have a question about entity proxies.
Given a generated Alert
entity class:
public partial class Alert
{
public Alert()
{
this.Readings = new HashSet<Reading>();
}
public int Id { get; set; }
public string Title { get; set; }
public virtual ICollection<Reading> Readings { get; set; }
}
The following unit test code passes:
using (var context = new MyContext())
{
var alert = context.Alerts.Create();
// Entity is a proxy
Assert.AreNotSame(entity.GetType(), ObjectContext.GetObjectType(entity.GetType()));
// Related entity collections are just HashSet<T>
Assert.AreSame(typeof(HashSet<Reading>), alert.Readings.GetType());
// Attach entity to the context
context.Alerts.Attach(alert);
var entry = context.ChangeTracker.Entries<Alert>().Single();
// Initially it's unchanged
Assert.AreEqual(EntityState.Unchanged, entry.State);
// Change a property
alert.Title = "Changed title";
// However it's still unchanged
Assert.AreEqual(EntityState.Unchanged, entry.State);
}
I've looked around online to try and find a definitive explanation of what generated proxy objects actually do. Some questions I have:
As far as I can tell, the association property getter/setter is overridden. What logic is added here? Is anything else done in the proxy?
The debugger shows a field called
_entityWrapper
of typeSystem.Data.Objects.Internal.EntityWrapperWithoutRelationships<System.Data.Entity.DynamicProxies.Alert_BF4E356370B8B5053A3384B5FAD30ECBA505359B71D47EBD90A674A9404D517C>
. What is this for?Will making attribute properties virtual do anything?