c# - 实体框架如何跟踪引用 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; }
}
解决方案
推荐阅读
- jquery - Jquery Bootgrid 有问题
- python - 如何在 python3 中为单个函数运行 doctest?
- json - 使用 JQ / pyjq 进行 JSON 转换。将对象从对象数组移动到另一个数组,由标识字段匹配
- node.js - 无法在没有 express-flash 的情况下设置会话 cookie
- python - 如何使用正则表达式从字符串中提取子字符串
- ruby-on-rails - Rails,Typescript:未捕获的 TypeError:无法读取未定义的属性“forEach”
- powershell - Powershell foreach 循环遍历从用户输入中拆分出来的列表
- blazor - Blazor WebAssembly - 从服务器端控制器方法写入 IDE 控制台
- java - 如何从 Java 字符串加载 EC 私钥?
- python - 合并具有多索引和列值的数据框