0

Let's say we have three tables:

  • Table1 is the principal table
  • Table2 and Table3 are dependent tables. They have 0..1<--->1 relation with Table1

  • Table1.id is an identity column, automatically generated by the database

  • Table2, 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 table2

    table2 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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Rohaitas
  • 84
  • 1
  • 12
  • 2
    Possible duplicate of [Using TransactionScope with Entity Framework 6](https://stackoverflow.com/questions/34083796/using-transactionscope-with-entity-framework-6) – Eris May 22 '17 at 18:14
  • 1
    Another alternative would be to use a `Sequence` instead. https://stackoverflow.com/questions/27077461/how-to-get-next-value-of-sql-server-sequence-in-entity-framework – Eris May 22 '17 at 18:25

1 Answers1

0

In the database you have a repeated foreign key to the same table.

user2648846
  • 375
  • 3
  • 3