I am absolutely new to Entity Framework so please don't hesitate to point any errors. Anyway I'll try to describe my problem as I understand it.
I am creating a n-Tier application with Entity Framework. I am also using a generic repository as described in this page.
I was successful in populating a DetailsView and inserting a value to the database through an ObjectDataSource. However the problem I am encountering occurs when I am updating.
The error that I get is
An object with a key that matches the key of the supplied object could
not be found in the ObjectStateManager. Verify that the key values of
the supplied object match the key values of the object to which
changes must be applied.
And I understand WHY it happens as well. I use the following methods to populate the values from the DB.
public IEnumerable<TEntity> GetAll<TEntity>() where TEntity : class
{
return GetQuery<TEntity>().AsEnumerable();
}
public List<Company> GetAllCompanies()
{
return _genericRepository.GetAll<Company>().ToList();
}
If I put a breakpoint in the GetAll method and observe the results I see that anEntityKey is set and IsChangeTracked is true for each object in the list.
But if I check the object passed to the UpdateMethod specified for the ObjectDataSource, then EntityKey is null, EntityState is detached and IsChangeTracked is false. So because the EntityKey is null, I understand the exception is valid.
My update method looks like
public void Update<TEntity>(TEntity entity) where TEntity : class
{
ObjectSet<TEntity> objSet = DataContext.CreateObjectSet<TEntity>();
objSet.ApplyCurrentValues(entity);
SaveChanges();
}
I tried to attach using objSet.Attach(entity); then no exceptions but the object does not get updated either.
How do I properly perform the update using DetailsView ? Or how do I properly bind an ObjectDataSource to a DetailsView? How do I make sure that my EntityKey does not become null ?
Solved the issue using the method shown below
public void Update<TEntity>(TEntity entity) where TEntity : class
{
object originalItem;
EntityKey key = DataContext.CreateEntityKey(GetEntityName<TEntity>(), entity);
if (DataContext.TryGetObjectByKey(key, out originalItem))
{
DataContext.ApplyCurrentValues(key.EntitySetName, entity);
}
SaveChanges();
}
Related
I'm using EF5 code first.
We have a method
LogHistoryTracking(DbEntityEntry entity)
to log changes when SaveChanges is called.
At SaveChanges, we get the changed entities and pass into LogHistoryTracking
var changedEntities = ChangeTracker.Entries().ToList();
But when I access
changedEntity.OriginalValues.PropertyNames
there is no properties for foreign keys object (only foreign key Id - but how can we get the data when there is only id here?).
I also tried to google for a solution, but this issue might be not so popular.
There is this article, but it does not work.
Appreciate any help. Thanks.
If you want to have your entity properties to be accessible you must 'Include' them prior to accessing them. Like in the following example which gets the orders of the first cutomer :
var orders = context.Customers
.Include("Orders")
.First().Orders;
In this example if you do not call .Include("Orders") you will not have Customer.Orders. The same goes if you have foreign key and forget to include the navigation property of the foreign key. This is because the key (the ID) is part of the object and the navigation property is not.
Let us see one real world example :
public class Employee : Entity
{
public virtual int CompanyUserId { get; set; }
public virtual CompanyUser CompanyUser { get; set; }
//... cut out for brevity
}
If you get the employees like this :
var employees = context.Employees;
You will not be able to access employees[0].CompanyUser after
context.SaveChanges() because of lazy loading. The connection is disposed after context.SaveChanges(), so no more data fetching.
But if you call :
var employees = context.Employees
.Include("CompanyUser")
.ToArray();
You will be able to access employees[0].CompanyUser.SomeProperty right away before context.SaveChanges regardless lazy loading because ToArray() will execute the query and fetch the entities with the "includes".
If you call :
var employees = context.Employees
.Include("CompanyUser");
Then you will have employee[0].CompanyUser.SomeProperty even after context.SaveChanges() with Lazy Loading because you have told EF to include "CompanyUser" property before executing the query. On execution EF will include the named property.
UPDATE
Intercepting DbContext can be done in at least two different ways.
First - override SaveChanges() or SaveChangesAsync because it is virtual:
public class MyDbContext : DbContext
{
public event Action<MyDbContext> SavingChanges = _ => { };
public override int SaveChanges()
{
this.SavingChanges(this);
return base.SaveChanges();
}
}
Second way without direct override is by hiding the DbContext inside interface like this one (this is from real project) :
public interface IUnitOfWork : IDisposable
{
void Commit();
}
Third way (somewhat different) is by intercepting the Db calls.
Fourth way exists but it depends on what IoC you use. If you use Castle Windsor you can use interceptors. I suppose that with every IoC there is its own way of intercepting this.
I was implementing audit in my EF 6 database by adding a ModifiedDate property in the entity base and tried to override SaveChanges() by adding the below code (Taken from https://stackoverflow.com/a/6282472/82152)
public class Session : ISession
{
public DbContext _context;
public Session(DbContext context)
{
_context = context;
}
.
.
.
public int SaveChanges()
{
var context = ((IObjectContextAdapter)_context).ObjectContext;
var objectStateEntries =
from e in context.ObjectStateManager.GetObjectStateEntries(EntityState.Modified)
where
e.IsRelationship == false &&
e.Entity != null &&
typeof(ModelBase).IsAssignableFrom(e.Entity.GetType())
select e;
foreach (var entry in objectStateEntries)
{
var modelBase = entry.Entity as ModelBase;
modelBase.LastModifiedDate = DateTime.Now;
}
return _context.SaveChanges();
}
After I changed one of the entity properties, and called SaveChanges - I could see that none of the objectStateEntries were marked as EntityState.Modified. It was all marked as EntityState.Unchanged.
Now after doing some reading, I changed my SaveChanges method to do context.DetectChanges(); after the first line - and it all worked. I tested a case where the entity property changed and another case where the entity property did not change, and it worked perfectly.
Now my concern is
Although my current solution works, would it take a performance hit ?
Why doesn't context.Configuration.AutoDetectChangesEnabled=true do the job of tracking the change automatically from property changes ?
You're not overriding SaveChanges(). You're creating a new wrapper class, and creating a SaveChanges method, and then trying to call SaveChanges() of your wrapped context. That's something totally different from overriding SaveChanges(). If you read the documentation for AutoDetectChangesEnabled You will find that it says this:
Gets or sets a value indicating whether the DetectChanges() method is called automatically by methods of DbContext and related classes. The default value is true.
So what it means is that DetectChanges() gets called when you call methods of the DbContext, which is something you are not doing. You ARE calling a method of the ObjectContext, which is not part of the DbContext (DbContext inherits from it), thus calling this method wouldn't trigger a DetectChanges() call.
It's quite self-explainatory.
I have a class that contains another
Let's call them Subject and Classroom
public class Subject
{
public Classroom Class {get; set;}
}
I'm using stateless facades, wich means my DbContext is disposed right after recovering the objects and is created to store the new ones.
Shouldn't it know that Classroom isn't a new object since it's ID is already in the DB?
Using the debugger I can track to the point right before I call the SaveChanges method and Classroom.id is the one I have on the database.
What's the problem? EF adds a new Classroom with the exact properties as the previous one, but with a new PK.
What am I doing wrong here?
This is the code used for the general CRUD operations (They are in my DbContext) Both update and delete work just fine:
public void Update(DbSet MySet, object Obj)
{
MySet.Attach(Obj);
var Entry = this.Entry(Obj);
Entry.State = EntityState.Modified;
this.SaveChanges();
}
public void Insert(DbSet MySet, object Obj)
{
MySet.Add(Obj);
this.SaveChanges();
}
public void Delete(DbSet MySet, object Obj)
{
MySet.Attach(Obj);
var Entry = this.Entry(Obj);
Entry.State = EntityState.Deleted;
this.SaveChanges();
}
Without seeing you're actual code on how you're either updating or creating your Subject entity, it's hard to tell. However, you're probably not attaching the Classroom so EF is assuming that the entity is new, when it's really not.
using (Model m = new Model())
{
m.Subject.Add(subject);
m.Classrooms.Attach(subject.Class);
m.SaveChanges();
}
Even though the PK is the same, without attaching to the Context, EF has no way of figuring out what you're intention is. Attaching the entity explicitly tells your context what you want.
I'd like to know how to create a method that will allow me to generically do this...
public class Repo<T> : IGenericRepo<T> where T : class
{
protected PteDotNetEntities db;
public T Get(int id)
{
//how do I dynamically get to the correct entity object and select it by
//id?
}
}
Yes you can. If you know that all your entities will have simple primary key property of type int and name Id you can do simply this:
public interface IEntity
{
int Id { get; }
}
All your entities must implement this interface. Next you can simply do:
public class Repo<T> : IGenericRepo<T> where T : class, IEntity
{
protected PteDotNetEntities db;
public T Get(int id)
{
return db.CreateObjectSet<T>().FirstOrDefault(e => e.Id == id);
}
}
This is the simplest possible solution. There are better solutions using GetObjectByKey but they are more complex. The difference between FirstOrDefault and GetObjectByKey is repeatable execution. FirstOrDefault always executes DB query whereas GetObjectByKey first checks if the entity with the same key was already loaded / attached to the context and returns it without querying the database. As reference for version using GetObjectByKey you can check similar questions:
Entity Framework Simple Generic GetByID but has differents PK Name
Generic GetById for complex PK
You can simplify those examples if you know the name of the key property upfront (forced by the interface).
In case of using code first / DbContext API you can also check this question:
Generic repository EF4 CTP5 getById
I'm wondering why there is no Detach method on the DbContext object like there is for ObjectContext. I can only assume this omission was intentional, but I have a hard time figuring out why. I need to be able to detach and re-attach entities (for putting in the cache in an ASP.NET project, for example). However, since I can't detach an entity, when I try to attach an entity that was associated with a previous context, I get the "An entity object cannot be referenced by multiple instances of IEntityChangeTracker" exception.
What's the guidance here? Am I missing something?
For people that might stumble upon this question, as of CTP5 you now need to write
((IObjectContextAdapter)context).ObjectContext
in order to get to ObjectContext.
DbContext uses an ObjectContext internally and EF team make this available as a protected property just in case you ever need to drop down to the lower level API and sounds like this is the case here, so you can use or expose the required functionality from a derived DbContext:
public class YourContext : DbContext
{
public void Detach(object entity)
{
ObjectContext.Detach(entity);
}
}
Then you can call this method from your controller to detach an entity.
Alternatively, you can change it to even have a richer API:
public class YourContext : DbContext
{
public void ChangeObjectState(object entity, EntityState entityState)
{
ObjectContext.ObjectStateManager.ChangeObjectState(entity, entityState);
}
}
Here is how DbContext looks like from metadata:
public class DbContext : IDisposable
{
protected System.Data.Objects.ObjectContext ObjectContext { get; }
...
}
EF:CF 4.1 RC1 and EF:CF 4.1 RTW have the same explicitly implemented IObjectContextAdapter:
public static class DbContextExtensions
{
public static void Detach(this System.Data.Entity.DbContext context, object entity)
{
((System.Data.Entity.Infrastructure.IObjectContextAdapter)context).ObjectContext.Detach(entity);
}
}
Microsoft decided "Detach is too advanced technology and should be hidden". IMHO the man who invented this should be shot - because if you add brand new entity, it is otherwise difficult to just remove it without commiting changes to db (you can manipulate with DbEntityEntry but that's another story).
Edit 4 years later:
With EF6 (i somehow skipped EF5 :) ) you dont need detach() anymore, becouse removing freshly added entry does not generate delete from [table] where [Id] = 0 as in EF4 - you can just call mySet.Remove(myFreshlyCreatedAndAddedEntity) and everything will be allright.
I usually extend the base class(inherits from the DbContext) with the property:
public class MyDbContext : DbContext
{
public ObjectContext ThisObjectContext
{
get
{
return ((IObjectContextAdapter)this).ObjectContext;
}
}
}
later you can use this property for variety of useful stuff ... like Detach :)