On my edit page, I was receiving this error:
Attaching an entity of type failed because another entity of the same type already has the same primary key value.
So, I researched that and came to this.
That lead me to want to use Auto-Mapper, to simplify things.
Here is my code:
if (db.TableName.Find(value.ID).stringProperty.Equals(value.stringProperty, StringComparison.CurrentCultureIgnoreCase))
{
Table inContextVariable = db.TableName.Find(value.ID);
Mapper.Initialize(config => config.CreateMap<ModelName, ModelName>());
Mapper.Map<ModelName, ModelName>(value, inContextVariable);
db.Entry(inContextVariable).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
This is leading me to this error:
Additional information: The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
This table has 1 Foreign Key, and when I debug that Foreign Key has a value of 11, so I am having a hard time understanding this error.
UPDATE
Imagine if I had a database table that held records containing a person's info such as:
| ID | First Name | Last Name | Email |
----------------------------------------------------------------------------
1 John Doe john.doe@test.com
2 Christopher Columbus | chris.columbus@test.com |
And so on and so on.. now, since a person can only have 1 email address, you have to have safeguards against other users when they are creating/editing records..
For example, if I were to use this application and I wanted to create an account.. I shouldn't be able to successfully fill out the form with an email address that already exists in the database table..I should receive an error message saying something like
That email address already exists! Please enter another email address
Here is that code:
if (db.TableName.Any(x => x.Email.Equals(value.Email, StringComparison.CurrentCultureIgnoreCase)))
{
ModelState.AddModelError("Email", "This email already exists!");
return View(value);
}
Now.... what happens when a user successfully creates an account.. then goes to edit their account email address to something that already exists? The same error message should appear, which it does.
BUT when that user created their account, their email address is now in the database table.. so when they go to the Edit page, with their original email, and they hit 'Save', the code will check that email against all email addresses in the database, and it will see that it is already in the database causing the error message to show (please change your email. .etc)..
So I am in need of trying to create an efficient way of checking to see when the user is on the edit page, and hits 'Save' with their original email that it doesn't tell them to change their email.
So for that I have this code:
if (db.TableName.Find(value.ID).Email.Equals(value.Email, StringComparison.CurrentCultureIgnoreCase))
{
var inContextVariable = db.TableName.Find(value.ID);
MappingMethods.MapModelName(inContextVariable , value);
\\ without the mapping.. I receive the error saying 'Attaching an entity of type failed because another entity of the same type already has the same primary key value.'
db.Entry(inContextVariable).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}