Let's say we have three tables:
Table1
is the principal tableTable2
andTable3
are dependent tables. They have 0..1<--->1 relation withTable1
Table1.id
is an identity column, automatically generated by the databaseTable2
,Table3
do not have an identity column
This will allow me to insert in principal entity Table1
without dependent. I read that when you make StoreGeneratedPattern = identity
. EF reads the value generated in the database and stores it in memory cache.
So I want to assign that Table1_id
to Table2
which is a dependent table. So my questions are
How would I get that
id
? I want to assign that id to table2table2 t2 = new Table2(){ id = ? }; // How?
Now coming back, another question is: do I have to assign
virtual
to null in controller?Table1 t1 = new Table1() { table2 = null, table3 = null } // ???
Table1
public partial class table1
{
public int Id { get; set; }
public string username { get; set; }
public virtual table2 table2 { get; set; }
public virtual table3 table3 { get; set; }
}
Another related question: I wanted Table1
to have either Table2
or Table3
related. So how would I connect them?