首页 > 解决方案 > 实体框架如何跟踪引用 ID

问题描述

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

实际上,我正在将数据从一个数据库迁移到另一个数据库。所以,我有两个具有主从关系的实体:1. QuestionMaster 2. ListName

QuestionMasterId 是 ListName 中的 FK。

现在,不必将每个问题都添加到列表中,因此,我必须继续将问题添加到 QuestionMaster 中,直到找到与列表相关联的问题,然后我必须创建 ListName 但应分配相应的 QuestionMasterId到列表名称。

下面将详细解释它,这是一个复杂的代码,我删除了一些代码以便更容易理解不要担心属性:

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; }
    }

标签: c#.netentity-frameworkidentityef-database-first

解决方案


推荐阅读