When I try to update a user profile in my app i get the following error:
The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_dbo.UserDetails_dbo.Companies_CompanyId". The conflict occurred in database "aspnet-eksp-20161223071733", table "dbo.Companies", column 'CompanyId'. The statement has been terminated.
Although it may look self explenatory I have no idea how to solve it and make my code work. I am very new with EF and the whole platform in general.
I do have a foreign key in my model, of course, and this foreing key field is not empty, it has a value.
Here is the model :
public class UserDetails
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UserDetailsId { get; set; }
public byte[] ImageData { get; set; }
[NotMapped]
public HttpPostedFileBase UploadImage { get; set; }
[NotMapped]
public string ImageBase64 => System.Convert.ToBase64String(ImageData);
public string FirstName { get; set; }
public string LastName { get; set; }
public string UserAddress { get; set; }
public string UserCountry { get; set; }
public string UserPostalCode { get; set; }
public string UserPhoneNumber { get; set; }
public int CompanyId { get; set; }
public virtual Company Company { get; set; }
public string identtyUserId { get; set; }
}
And the controller method :
public ActionResult Edit([Bind(Include = "UserDetailsId,ImageData,FirstName,LastName,UserAddress,UserCountry,UserPostalCode,UserPhoneNumber,CompanyId,identtyUserId")] UserDetails userDetails, HttpPostedFileBase UploadImage)
{
if (ModelState.IsValid)
{
byte[] buf = new byte[UploadImage.ContentLength];
UploadImage.InputStream.Read(buf, 0, buf.Length);
userDetails.ImageData = buf;
db.Entry(userDetails).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
//ViewBag.CompanyId = new SelectList(db.Companies, "CompanyId", "CompanyName", userDetails.CompanyId);
return View(userDetails);
}
In the view I can update everything but the last two fields(in the model), but i doubt this is the problem.
Any help will be appreciated.
Hi Can you tru this . please replace this code under you if statement .
byte[] buf = new byte[UploadImage.ContentLength];
UploadImage.InputStream.Read(buf, 0, buf.Length);
userDetails.ImageData = buf;
--- here first read the dataof edited userDetail get it from context and chage this object state to modified and thn save.
var currentUserDetail = db.UserDetails.FirstOrDefault(f=>f.UserDetailid= Id);
currentUserDetail.FirstName= userDetails.userDetails;
db.Entry(currentUserDetail).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
Your UserDetails_dbo
table has a foreign key column named Companies_CompanyId
. You are sending an updated record to the database, a UserDetails_dbo
record but the value of the Companies_CompanyId
does not exist in the database. Therefore, you are getting that error.
Make sure the updated record you are sending has a value for the foreign key and make sure the value exists in the database.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.