I have 2 tables in an existing database:
Table1
[Key] public int Id {get; set;}
public int CustomerID {get; set;}
List<Table2> Table2Items {get; set;}
....
Table2
[Key] public int Id {get; set;}
public int customerid {get; set;}
Table1 Table1Item {get; set;}
...
I want to create a one-to-many relationship, such that each record in table1 can have many associated records in table2.
Its normally straight forward using the primary key field in table1 which matches the foreign key field (customerid) in table 2.
But I want to relate the 2 tables based on the CustomerID in table1 with the customerid in table2.
The following appears to relate the 2 tables by using the customerid field in table2 with the primary key in table1, which is not what I require.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Table1>()
.HasMany<Table2>(s => s.Table2Items)
.WithRequired(s => s.Table1Item)
.HasForeignKey(s => s.customerid);
}
How can I modify the code shown above to fit my requirements.