我知道这已经被问过数百万次了,我自己也有过数百次,但由于某种原因,我无法解决这个问题。
我得到了众所周知的错误:
The UPDATE statement conflicted with the FOREIGN KEY constraint ...
当插入或删除完成时,我数据库中的所有表都会级联。
现在出现错误:我想更新链接到文化表(用于语言)的管理员表(管理员帐户)。
一切都正确填写。因此我们得到以下代码:
[HttpPost]
public ActionResult Edit(Admins admins)
{
if (!ModelState.IsValid)
{
return View(admins);
}
admins.cultures_id = admins.Cultures.id;
_unitOfWork.AdminsRepository.Update(admins);
_unitOfWork.Save();
return RedirectToAction("Index", "Overview", new { area = "Admin" });
}
我首先将我的管理对象/实体的文化 ID 设置为与链接的文化表中的 ID 相同:
admins.cultures_id = admins.Cultures.id;
然后我填写更新表格:
_unitOfWork.AdminsRepository.Update(admins);
方法更新包含以下代码:
public virtual void Update(TEntity entityToUpdate)
{
DbSet.Attach(entityToUpdate);
ArtWebShopEntity.Entry(entityToUpdate).State = EntityState.Modified;
}
到目前为止一切顺利,但是,当我真正想保存管理员时:
_unitOfWork.Save();
该保存方法包含以下代码:
public void Save() {
try
{
_artWebshopEntity.SaveChanges();
}
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", validationErrors.Entry.Entity.GetType().Name, validationErrors.Entry.State);
foreach (var validationError in validationErrors.ValidationErrors)
{
Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", validationError.PropertyName, validationError.ErrorMessage);
}
}
throw; // Will do something here later on...
}
}
在 SaveCHanges 方法中,我得到了错误。我知道这意味着什么,但我似乎无法修复它。我已经尝试了所有我知道可能导致它的事情。
编辑
我只想更新管理值,所以我不想更新文化值。
这是查询:
update [dbo].[Admins]
set [login] = 'Herve' /* @0 */,
[password] = null,
[salt] = null,
[email] = 'xxxxx.xxx@glevin.be' /* @1 */,
[permissions] = 'administrator' /* @2 */,
[attempts] = 4 /* @3 */,
[locked] = 0 /* @4 */,
[cultures_id] = 0 /* @5 */
where ([id] = 1 /* @6 */)
所以,cultures_id 是问题所在。我现在做了以下事情:
var updateAdmin = new Admins
{
attempts = admins.attempts,
cultures_id = admins.cultures_id,
email = admins.email,
locked = admins.locked,
login = admins.login,
id = admins.id,
password = admins.password,
permissions = admins.permissions,
salt = admins.salt,
};
这很有效,但是当我将 Cultures 对象添加到组合中时,它会崩溃并给我参考错误。所以归结为,我如何将具有外键的表更新到另一个表也需要更新?
编辑二
我的管理员和文化实体(数据库优先),也是 sql management studio 中的数据库图像:
管理类:
public partial class Admins
{
public int id { get; set; }
public string login { get; set; }
public string password { get; set; }
public string salt { get; set; }
public string email { get; set; }
public string permissions { get; set; }
public Nullable<int> attempts { get; set; }
public Nullable<bool> locked { get; set; }
public Nullable<int> cultures_id { get; set; }
public virtual Cultures Cultures { get; set; }
}
文化课:
public partial class Cultures
{
public Cultures()
{
this.Categories_local = new HashSet<Categories_local>();
this.Menu_items_local = new HashSet<Menu_items_local>();
this.Products_local = new HashSet<Products_local>();
this.Subcategories_local = new HashSet<Subcategories_local>();
this.Webpages_local = new HashSet<Webpages_local>();
this.Admins = new HashSet<Admins>();
}
public int id { get; set; }
public string name { get; set; }
public string display_name { get; set; }
public virtual ICollection<Categories_local> Categories_local { get; set; }
public virtual ICollection<Menu_items_local> Menu_items_local { get; set; }
public virtual ICollection<Products_local> Products_local { get; set; }
public virtual ICollection<Subcategories_local> Subcategories_local { get; set; }
public virtual ICollection<Webpages_local> Webpages_local { get; set; }
public virtual ICollection<Admins> Admins { get; set; }
}