I am new to Entity Framework and I am working with a db first model and I am attempting to store some data. I have a table (table1) that is related to another table (table2), so EF created a navigation property between the two. Do I set the navigation property within table1 or do I add a new entity to insert table2 with the record id of table1? Table2 has a foreign key to Table1's id field. I need table2 to populate that. Also from my understanding the id field will be set by EF as an incremental int value?
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 = ???
}}
}
}