これは何百万回も聞かれていることを知っていますし、私自身も何百回も経験しましたが、何らかの理由でこれを修正することはできません.
よく知られているエラーが表示されます:
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(管理者);
メソッド update は次のコードを保持します。
public virtual void Update(TEntity entityToUpdate)
{
DbSet.Attach(entityToUpdate);
ArtWebShopEntity.Entry(entityToUpdate).State = EntityState.Modified;
}
これまでのところは順調ですが、実際に管理者を保存したい場合:
_unitOfWork.Save();
その 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; }
}