Is it possible to see added entities from an unsaved EF4 context?
I'm just getting into Entity Framework 4, and was eventually hoping to wrap it in a repository pattern using POCOs. I discovered something that I wasn't expecting though. It appears that if you create a context, add an object to it (without saving the context) and query the context again, it doesn't include the new object in the results. Am I doing something wrong? It seems like it should return what I've added, even if I haven't saved the results back to the database yet. Here's my sample code:
ShopEntities context = new ShopEntities();
// there is only 1 customer so far
var customers = from c in context.Customers
select c;
Console.WriteLine(customers.Count()); // displays 1
Customer newCustomer = context.Customers.CreateObject();
newCustomer.FirstName = "Joe";
newCustomer.LastName = "Smith";
conte开发者_开发百科xt.Customers.AddObject(newCustomer);
var customers2 = from c in context.Customers
select c;
Console.WriteLine(customers2.Count()); // still only displays 1
context.SaveChanges();
var customers3 = from c in context.Customers
select c;
Console.WriteLine(customers3.Count()); // only after saving does it display 2
An L2E query always returns entities from the DB. It will merge them with in-memory changes based on the query's MergeOption
.
To see added entities, look at the context:
var addedCustomers = from se in context.ObjectStateManager.GetObjectStateEntries(EntityState.Added)
where se.Entity is Customer
select se.Entity;
Use ChangeTracker property:
public delegate void SaveEventHandler(Dictionary<EntityState, List<IAuditable>> entities);
public event SaveEventHandler OnReturnAuditableEntitiesAfterSaveEvent;
public override Task<int> SaveChangesAsync()
{
try
{
if (OnReturnAuditableEntitiesAfterSaveEvent != null)
{
List< EntityState > states = new List<EntityState>()
{
EntityState.Added,
EntityState.Deleted,
EntityState.Modified
};
Dictionary<EntityState, List<IAuditable>> entities = new Dictionary<EntityState, List<IAuditable>>();
List<DbEntityEntry> unsavedEntities = ChangeTracker.Entries()
.Where(p => p.Entity is IAuditable && states.Contains(p.State))
.ToList();
foreach (var state in states)
{
List<DbEntityEntry> list = unsavedEntities.Where(c => c.State == state).ToList();
if (list.Count > 0)
{
switch (state)
{
case EntityState.Added:
case EntityState.Deleted:
entities.Add(state, list.Select(c=> c.Entity as IAuditable).ToList());
break;
case EntityState.Modified:
List<IAuditable> modifiedAntities = new List<IAuditable>();
//Check for modified entities if the value has changed
foreach (var entry in list)
{
foreach (var prop in entry.OriginalValues.PropertyNames)
{
var originalValue = entry.OriginalValues[prop].ToString();
var currentValue = entry.CurrentValues[prop].ToString();
if (originalValue != currentValue)
{
modifiedAntities.Add(entry.Entity as IAuditable);
}
}
}
if (modifiedAntities.Count > 0)
entities.Add(state, modifiedAntities);
break;
default:
break;
}
}
}
if (entities.Count > 0)
OnReturnAuditableEntitiesAfterSaveEvent.Invoke(entities);
}
return base.SaveChangesAsync();
}
catch (DbEntityValidationException ex)
{
throw CollectValidationErrors(ex);
}
}
精彩评论