我是Entity Framework的新手,并且我正在使用数据库优先模型,并且尝试存储一些数据。我有一个与另一个表(table2)相关的表(table1),因此EF在两者之间创建了一个导航属性。我是在table1内设置导航属性还是添加一个新实体以插入具有table1记录ID的table2? Table2具有Table1的ID字段的外键。我需要用table2填充它。另外据我了解,EF将id字段设置为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 = ???
}}
}
}