c# - EF6 数据库优先:如何将数据保存到集合导航属性?
问题描述
我是 Entity Framework 的新手,我正在使用 db first 模型,我正在尝试存储一些数据。我有一个与另一个表 (table2) 相关的表 (table1),因此 EF 在两者之间创建了一个导航属性。我是在 table1 中设置导航属性,还是添加一个新实体以插入 table2,记录 id 为 table1?Table2 有一个指向 Table1 的 id 字段的外键。我需要 table2 来填充它。另外据我了解, id 字段将由 EF 设置为增量 int 值吗?
public partial class Table1
{
public int Id1 {get; set;}
public virtual ICollection<Table2> Table2s {get; set;}
}
public partial class Table2
{
public int Id2 {get; set;}
public int somedata {get; set;}
public int Id1 {get; set;}
public virtual Table1 Table1 {get; set;}
}
using(TablesEntities context = new TablesEntities())
{
var data = new Table1
{
Table2s = new [] { new Table2s
{
somedata = 123,
id1 = ???
}}
}
}
解决方案
推荐阅读
- angular - TypeError,表单验证器
- python - 接受回调的 Telegram 机器人的固定按钮
- javascript - 我可以将句柄功能作为道具传递给路由器中的其他屏幕吗?
- python - Python unittest:如何将标准输出消息临时重定向到缓冲区并测试其内容?
- android - Android:保持视图可见性逻辑的正确位置
- python - Xlwings python pandas dataframe groupby返回重复数据
- facebook - Facebook 广告跟踪
- c# - ASP.NET Core 2.2 如何从自定义 UserManager 调用自定义 UserValidator
- gradle - 为什么 IntelliJ 找不到 org.gradle.api 库,而 OSX CLI 可以?
- javascript - jQuery:onClick 不可重用