C# 实体框架如何跟踪引用Id

C# 实体框架如何跟踪引用Id,c#,.net,entity-framework,identity,ef-database-first,C#,.net,Entity Framework,Identity,Ef Database First,我有点困惑,实体框架如何跟踪任何对象的引用Id 实际上,我正在将数据从一个数据库迁移到另一个数据库。因此,我有两个实体具有主-细节关系: 1.提问者 2.列表名 ListName中QuestionMasterId为FK 现在,没有必要将每个问题都添加到列表中,因此,我必须继续将问题添加到QuestionMaster中,直到我找到一个与列表关联的问题,然后我必须创建ListName,但相应的QuestionMasterId应该分配给ListName 下面将详细解释它,这是一个复杂的代码。我删除了一

我有点困惑,实体框架如何跟踪任何对象的引用Id

实际上,我正在将数据从一个数据库迁移到另一个数据库。因此,我有两个实体具有主-细节关系: 1.提问者 2.列表名

ListName中QuestionMasterId为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; }
    }
公共部分类ListName
{
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2214:DoNotCallOverridableMethodsInConstructors”)]
公共列表名()
{
this.ListItems=new HashSet();
this.QuestionLists=newhashset();
}
公共int Id{get;set;}
public int QuestionId{get;set;}
公共字符串代码{get;set;}
公共字符串ListNameText{get;set;}
公共字符串是活动的{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection列表项{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection问题列表{get;set;}
}
公共部分类提问器
{
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2214:DoNotCallOverridableMethodsInConstructors”)]
公共质询主任()
{
this.QuestionLists=newhashset();
}
公共int Id{get;set;}
公共可为空的QuestionId{get;set;}
公共字符串问题代码{get;set;}
公共字符串QuestionType{get;set;}
公共字符串QuestionText{get;set;}
公共可为空的OrderNb{get;set;}
公共字符串是活动的{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection问题列表{get;set;}
}

为什么你评论了
\\QuestionMasterId=\u questionMaster\u new.Id
?如果你把模型类共享为well@TJWolschon,实际上,我正在一次保存所有实体批处理,所以当您保存每个实体时,它会工作,因此它是无用的,让我删除它。当然,我也会添加模型类。