我有点困惑,实体框架如何跟踪任何对象的引用ID。
实际上,我正在将数据从一个数据库迁移到另一个数据库。所以,我有两个具有主要细节关系的实体: 1.问题主持人 2. ListName
QuestionMasterId是ListName中的FK。
现在,每个问题都没有必要列表,因此,我必须继续将问题添加到QuestionMaster中,直到我找到一个与列表相关联的问题,然后我必须创建ListName但是相应的QuestionMasterId应该分配给ListName。
下面将详细解释它,这是一个复杂的代码我删除了一些代码,使其更容易理解,不用担心属性:
foreach (var Question in _tshQuestionMaster_local)
{
// adding new question
DataAccess.Hotel.DataModel.QuestionMaster _questionMaster_new = new DataAccess.Hotel.DataModel.QuestionMaster()
{
QuestionType = Question.TypeCode,
QuestionText = Question.QuestionText,
OrderNb = Question.OrderNo,
Isactive = "Y"
};
_hotelEntities.QuestionMasters.Add(_questionMaster_new);
if (IsQuestionHasList() && IsNewList())
{
//adding new list name
ListName _listName_new = new ListName
{
IsActive = "Y",
QuestionMaster = _questionMaster_new
};
_hotelEntities.ListNames.Add(_listName_new);
}
}
_hotelEntities.SaveChanges();
现在,假设有10个问题,其中列表仅与问题编号3和8相关联。我应该将ListName中的QuestionMasterId设置为3和8但是它显示我1和2这让我觉得EF没有保留参考文献。这是真的吗?如果是,那么有什么可以替代获得相应的ID?
我的模型类是:
public partial class ListName
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public ListName()
{
this.ListItems = new HashSet<ListItem>();
this.QuestionLists = new HashSet<QuestionList>();
}
public int Id { get; set; }
public int QuestionId { get; set; }
public string Code { get; set; }
public string ListNameText { get; set; }
public string IsActive { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ListItem> ListItems { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<QuestionList> QuestionLists { get; set; }
}
public partial class QuestionMaster
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public QuestionMaster()
{
this.QuestionLists = new HashSet<QuestionList>();
}
public int Id { get; set; }
public Nullable<int> QuestionId { get; set; }
public string QuestionCode { get; set; }
public string QuestionType { get; set; }
public string QuestionText { get; set; }
public Nullable<int> OrderNb { get; set; }
public string Isactive { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<QuestionList> QuestionLists { get; set; }
}