我正在開發一個在VS 2012中使用EF 5的解決方案,我對添加和更新實體時指定實體關係的正確方式感到困惑。更新實體框架中的分離實體以及相關實體
這是我的主要類別,其中notifier
是person
:
public class Notifier : Person
{
public bool IsValid { get; set; }
public int NotifierTypeID { get; set; }
public virtual NotifierType NotifierType { get; set; }
public int MyCaseID { get; set; }
public virtual MyCase MyCase { get; set; }
}
public abstract class Person
{
public int PersonID { get; set; }
public String Name { get; set; }
}
通告屬於個案
public class MyCase
{
public int MyCaseID { get; set; }
public DateTime DateOfNotification { get; set; }
public virtual ICollection<Notifier> Notifiers { get; set; }
}
又有型:
public class NotifierType
{
public int NotifierTypeID { get; set; }
public string NotifierTypeName { get; set; }
}
我露出了國外通知者和案例與通知器類型之間的關鍵字。
我使用的方法添加/更新通知是:
using (MyContext dbContext = new MyContext(connectionString))
{
notifier.MyCaseID = MyCaseID;
notifier.NotifierTypeID = notifierView.NotifierTypeID;
// **** the puzzling line ****
notifier.NotifierType = dbContext.NotifierTypes.Find(notifierView.NotifierTypeID);
//dbContext.Database.Log = s => System.Diagnostics.Debug.Write(s);
dbContext.Entry(notifier).State = notifier.PersonID == 0 ? EntityState.Added : EntityState.Modified;
dbContext.SaveChanges();
// save the ID in case it's new
notifierViewReturn.PersonID = notifier.PersonID;
}
我對線以上的評論**** the puzzling line ****
後不解。我明確指定外鍵,如果我添加通告程序,則不需要此行,但如果我正在更新對象,則確實需要它,否則會引發異常。
唯一的例外是
Message=A referential integrity constraint violation occurred:
The property values that define the referential constraints are not
consistent between principal and dependent objects in the relationship.
誰能請解釋爲什麼需要在所有的這條線。謝謝
你應該發佈拋出的異常。從這個角度來看,你可能會猜到接下來你需要看什麼(也就是說,在你習慣了EF例外的意思之後)。 – rliu
嗯。這可能稍微不相關,但我對'Notifier'的PK有點困惑。我認爲它是'PersonID',除了你也有一個'EditedByID = thisUser.PersonID' ...那些PK是同一個表嗎?另外,您是否可以確認'Notifier'和'NotifierType'之間的FK是否在數據庫的'Notifier'表中? – rliu
Hi Roliu。是的PK是PersonID - 我已經刪除了EditById,因爲它是無關緊要的。 FK在數據庫中。 –