I use repository with Update method. In this method I need to check some conditions, finding updating entity in DB before actual update. I use one context for both - Find and Update operations inside one transaction. But when I try to update entity I get an exception:
"Attaching an entity of type 'MyNamespace.MyEntityType' failed because another entity of the same type already has the same primary key value"
public void Update(SomeType entity)
{
using (var context = new MyEntities())
{
using (var transaction = context.Database.BeginTransaction(IsolationLevel.RepeatableRead))
{
try
{
// check conditions
var root = context.MyEntitySet
.Where(e => e.Parent == null); // entity.Parent == null
if(root ...)
; // return
// HERE I got an error described above
context.Entry(entity).State = EntityState.Modified;
context.SaveChanges();
transaction.Commit();
}
catch
{
transaction.Rollback();
throw;
}
}
}
}
I need to load entity with same Id from DB first, check some conditions and if ok, update entity. But after "Get" entity attaches to ChangeTracker with "Detached" state and when I try to change state to "Modified" I got an error.
I've already tried:
if (IsAttached(entity, out attachedEntity))
context.Entry(attachedEntity).CurrentValues.SetValues(entity);
but my original entity object stays unchanged with DB generated values. State = EntityState.Modified works as I need, but how can I remove preloaded entity from ChangeTracker?
Related
I am trying to use Entity Framework for work with database, I use Extension Method and pass Entity Context Into Logic Codes, The Database is update successful, but when I call back, result, I still old Records, I guess that issue on Cache of Entities, But It not make clear, I could not find any thing wrong in my code. Please help:
Extension method:
public static bool UpdateTruck(this Truck Truck, Truck updateInfo, Entities entities)
{
var isSuccess = true;
try
{
// Find Enity Object
var ObjectModel = entities.Truck.Where(x => x.Code == Truck.Code && x.CodePlant == Truck.CodePlant).FirstOrDefault();
// Mapping Modified Properties
ObjectModel = Mapper.Map(updateInfo, ObjectModel);
// Create Database Entity Transaction
entities.Truck.AddOrUpdate(ObjectModel);
//Save Changes
entities.SaveChanges();
}
catch (Exception exception)
{
Debug.WriteLine("[Application Exception:] " + exception.Message);
isSuccess = false;
}
return isSuccess;
}
Here is where I call: if (truckInfo.FindTruck(entities).UpdateTruck(truckInfo,entities))
After that, i checked database, i value update success update, but when I call:
using (Entities entities = new Entities())
{
PageModel.Truck= Truck.FindTruck(entities);
....
It receive old record.
Entity Framework caches objects internally in a DbContext instance.
Look here
Your issue isn't caching. You are using the AddOrUpdate method, which is only designed for use when initialising a database with seed data.
Use:
entities.Truck.Attach(Truck); // if it already exists.
// since you are loading it again for some reason
var ObjectModel = entities.Truck.Where(x => x.Code == Truck.Code && x.CodePlant == Truck.CodePlant).FirstOrDefault();
// Mapping Modified Properties
ObjectModel = Mapper.Map(updateInfo, ObjectModel); // this looks dubious
// You've just loaded it from the context so it's tracked, so you could just save it
entities.SaveChanges();
I am trying to update an entity's FK. I am doing it like this:
myEntity.FK_ID= Guid.Parse("aff4b5d9-e197-4f0d-85ff-b04464d9e4f0"); //Entity with this Guid exists inside the DB
myEntity.ForeignKeyProperty= null;
Then I try to update the entity (new context) like this:
using (MyContext db = new MyContext ())
{
//db.Benutzer.Attach(entity); // <-- Same Exception
db.Entry(entity).State = EntityState.Modified;
}
This results in a InvalidOperationException:
A referential integrity constraint violation occurred: The property value(s) of 'MyTable.MyTable_ID' on one end of a relationship do not match the property value(s) of 'MyEntity.FK_ID' on the other end.
I really don't know why. I then tried the following:
I changed the value of FK_ID to the target value inside the Database. This worked (okay, EF chaching some stuff maybe...)
But then I rechanged the value to something else and it STILL works.
After the manual DB change it now works for this one entity EVERYTIME. If I try to update another entity I get the same Exception. Any ideas on why this is happening?!
Regards,
Marcel
EDIT:
Here's a more complete Code:
//In MVC-Controller
Benutzer dbBenutzer = Database.GetBenutzerByID(model.ID_Benutzer);
// ... Do some other changes to the entity
dbBenutzer.ChangeMitarbeiter = null;
dbBenutzer.MitarbeiterChange_ID = CurrentUser.Mitarbeiter_ID;
//...SaveChanges via
Database.Update<Benutzer>(dbBenutzer);
// In Repository
public static int Update<E>(E entity) where E : class
{
using (Context db = new MMVContext())
{
try
{
db.Entry(entity).State = EntityState.Modified; //EXCEPTION
return SaveRepositoryChanges<E>(db);
}
catch (Exception e)
{
Logging.Error(e.ToExtString());
return -1;
}
}
}
I'm trying to copy an entity from one DbContext to another.
So I'm loading and detaching the entity before calling the save methods of the other DbContext.
EDIT:
First of all I'm loading the entity with the ID of the UserDT, then I'm detaching it from the old DbContext.
Finally I'l set SET IDENTITY_INSERT tUsers ON, save the entity and then turn SET IDENTITY_INSERT tUsers OFF.
var userEntity = DataContext.GetById<User>(id);
DataContext.EnsureDetached(userEntity);
offlineContext.ToggleIdentityInsert(true, typeof(User));
offlineContext.SetSaved(userEntity);
offlineContext.SaveChanges();
offlineContext.ToggleIdentityInsert(false, typeof(User));
Before DbContext.SaveChanges() I call this method (please note that this method is in a class inheriting from DbContext):
public T SetSaved<T>(T obj) where T : class, IEntity
{
var isNew = base.Set<T>().All(t => t.Id != obj.Id);
T ret = default(T);
var entry = Entry(obj);
if (isNew)
{
entry.State = EntityState.Added;
ret = base.Set<T>().Add(obj);
}
else
{
entry.State = EntityState.Modified;
}
return ret;
}
All entities I've used so far worked with this method.
Our User entity causes a NullReferenceException when calling this method, because the the properties CurrentValues and OriginalValues throw an InvalidOperationException saying that the entity is not attached to the current DbContext.
With this problem I can access the State and then add the obj to the DbContex.
EDIT:
I've forgot to say that the above code works on the second try. In my scenario I'm pressing the login button and the logged in user should be copied (offline DB). The first time I'll get the error, the second time I press the login button everything works fine.
Do you know what I could have forgotten?
Error message: Attaching an entity of type failed because another entity of the same type already has the same primary key value.
Question: How do I attached an entity in a similar fashion as demonstrated in the AttachActivity method in the code below?
I have to assume the "another entity" part of the error message above refers to an object that exists in memory but is out of scope (??). I note this because the Local property of the DBSet for the entity type I am trying to attach returns zero.
I am reasonably confident the entities do not exist in the context because I step through the code and watch the context as it is created. The entities are added in the few lines immediately following creation of the dbcontext.
Am testing for attached entities as specified here:what is the most reasonable way to find out if entity is attached to dbContext or not?
When looking at locals in the locals window of visual studio I see no entities of type Activity (regardless of ID) except the one I am trying to attach.
The code executes in this order: Try -> ModifyProject -> AttachActivity
Code fails in the AttachActivity at the commented line.
Note the code between the debug comments which will throw if any entities have been added to the context.
private string AttachActivity(Activity activity)
{
string errorMsg = ValidateActivity(activity); // has no code yet. No. It does not query db.
if(String.IsNullOrEmpty(errorMsg))
{
// debug
var state = db.Entry(activity).State; // Detached
int activityCount = db.Activities.Local.Count;
int projectCount = db.Activities.Local.Count;
if (activityCount > 0 || projectCount > 0)
throw new Exception("objects exist in dbcontext");
// end debug
if (activity.ID == 0)
db.Activities.Add(activity);
else
{
db.Activities.Attach(activity); // throws here
db.Entry(activity).State = System.Data.Entity.EntityState.Modified;
}
}
return errorMsg;
}
public int ModifyProject(Presentation.PresProject presProject, out int id, out string errorMsg)
{
// snip
foreach (PresActivity presActivity in presProject.Activities)
{
Activity a = presActivity.ToActivity(); // returns new Activity object
errorMsg = ValidateActivity(a); // has no code yet. No. It does not query db.
if (String.IsNullOrEmpty(errorMsg))
{
a.Project = project;
project.Activities.Add(a);
AttachActivity(a);
}
else
break;
}
if (string.IsNullOrEmpty(errorMsg))
{
if (project.ID == 0)
db.Projects.Add(project);
else
db.AttachAsModfied(project);
saveCount = db.SaveChanges();
id = project.ID;
}
return saveCount;
}
This is the class that news up the dbContext:
public void Try(Action<IServices> work)
{
using(IServices client = GetClient()) // dbContext is newd up here
{
try
{
work(client); // ModifyProject is called here
HangUp(client, false);
}
catch (CommunicationException e)
{
HangUp(client, true);
}
catch (TimeoutException e)
{
HangUp(client, true);
}
catch (Exception e)
{
HangUp(client, true);
throw;
}
}
I am not asking: How do I use AsNoTracking What difference does .AsNoTracking() make?
One solution to avoid receiving this error is using Find method. before attaching entity, query DbContext for desired entity, if entity exists in memory you get local entity otherwise entity will be retrieved from database.
private void AttachActivity(Activity activity)
{
var activityInDb = db.Activities.Find(activity.Id);
// Activity does not exist in database and it's new one
if(activityInDb == null)
{
db.Activities.Add(activity);
return;
}
// Activity already exist in database and modify it
db.Entry(activityInDb).CurrentValues.SetValues(activity);
db.Entry(activityInDb ).State = EntityState.Modified;
}
Attaching an entity of type failed because another entity of the same type already has the same primary key value. This can happen when using the Attach method or setting the state of an entity to Unchanged or Modified if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the Add.
The solution is that
if you had to use GetAll()
public virtual IEnumerable<T> GetAll()
{
return dbSet.ToList();
}
Change To
public virtual IEnumerable<T> GetAll()
{
return dbSet.AsNoTracking().ToList();
}
I resolved this error by changing Update method like below.
if you are using generic repository and Entity
_dbContext.Set<T>().AddOrUpdate(entityToBeUpdatedWithId);
or normal(non-generic) repository and entity , then
_dbContext.Set<TaskEntity>().AddOrUpdate(entityToBeUpdatedWithId);
If you use AddOrUpdate() method, please make sure you have added
System.Data.Entity.Migrations namespace.
I have the following method:
public static void UpdatePpsTransaction(IEnumerable<PpsTransaction> ppsTransaction)
{
using (var context = PpsEntities.DefaultConnection())
{
foreach (var trans in ppsTransaction)
{
context.PpsTransactions.Attach(trans);
}
context.SaveChanges();
}
}
}
I was removing these records but I ended up creating a field IsProcessed which I am now setting to true. Now I am updating the records instead of deleting them, keeping them for record keeping.
Anyhow, I am not getting any errors but it is not updating the record.
Any suggestions?
You're not telling EF that you have made any changes, try to use the Entry method, on your Context:
public static void UpdatePpsTransaction(IEnumerable<PpsTransaction> ppsTransaction)
{
using (var context = PpsEntities.DefaultConnection())
{
foreach (var trans in ppsTransaction)
{
context.Entry(trans).State = EntityState.Modified;
}
context.SaveChanges();
}
}
This way, the entities will be attached to the context in a modified stated, so when you call SaveChanges(), these will be saved.
This will only work if the entities already exists in the database, which they should.
From msdn:
If you have an entity that you know already exists in the database but which is not currently
being tracked by the context then you can tell the context to track
the entity using the Attach method on DbSet. The entity will be in the
Unchanged state in the context. Note that no changes will be made to the
database if SaveChanges is
called without doing any other manipulation of the attached entity.
This is because the entity is in the Unchanged state.
Here is the link