Attaching an entity of type 'Scheduler.Models.UserImage' 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.
Error Updating an entity with relation 1 to 1:
I have two tables User and UserImage with an relation 0..1. I want to stores pictures of the Users in my application in a different table.
The User Class:
public partial class User
{
public User()
{
this.FirstName = "";
this.LastName = "";
this.SurName = "";
this.Mail = "";
this.City = "";
this.IsManager = false;
this.IsActive = false;
this.UserLevel = 0;
this.PhoneNumber = "";
this.SiteMembers = new ObservableCollection<SiteMember>();
this.GroupMembers = new ObservableCollection<GroupMember>();
}
public int User_ref { get; set; } // PK Autoinc
public string FirstName { get; set; }
public string LastName { get; set; }
public string SurName { get; set; }
public string Mail { get; set; }
public string City { get; set; }
public bool IsManager { get; set; }
public bool IsActive { get; set; }
public int UserLevel { get; set; }
public string PhoneNumber { get; set; }
public string Password { get; set; }
public virtual ObservableCollection<SiteMember> SiteMembers { get; set; }
public virtual ObservableCollection<GroupMember> GroupMembers { get; set; }
public virtual UserImage UserImage { get; set; }
}
The UserImage Class:
public partial class UserImage
{
public int Image_Ref { get; set; }// Auto Inc
public int User_Ref { get; set; } //Primary KEy
public byte[] Image { get; set; }
public virtual User User { get; set; }
}
I added entities to the recording DbSets.
var entity = new User();
DbSet<User> dbSet = db.Set<User>();
dbSet.Add(entity);
db.Entry(entity).State = EntityState.Added;
var entity = new UserImage();
DbSet<UserImage> dbSet = db.Set<UserImage>();
dbSet.Add(entity);
db.Entry(entity).State = EntityState.Added;
I modify some controls in the form and call save changes in order to generate the primary keys. After the savechanges I have thise code:
var query = db.UserImages.Include(e => e.User);
UserImage usrImg = query.AsNoTracking()
.Where(e => e.User_Ref == CurrentEntity.User_ref).FirstOrDefault();
db.Entry(usrImg).Entity.Image = UserImageSelection.Image;
db.Entry(usrImg).State = EntityState.Modified;//Over here crashes
"Attaching an entity of type 'Scheduler.Models.UserImage' 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. "
Where CurrentEntity.User_Ref is actually the User Table.What am I doing wrong here and why cannot modify the UserImage table when in insert mode.