0

I've got a problem with mapping relations in existing database.

I have two tables:

  • table1
    • table1Id (primary key)
    • table1Code
    • table1Field
  • table2
    • table2Id (primary key)
    • table1Code
    • table2Field

And I've a one-to-many relation between table2 and table1. So I need in table2 object table2.table1 which will be filled by looking for table1 row in table1 by table1Code. Of course I need ICollection<table2> inside table1.

There is reference in database:

ALTER TABLE table2 WITH CHECK ADD CONSTRAINT [FK_table2_table1]
    FOREIGN KEY([table1Code]) REFERENCES table1 ([table1Code])

I use EntityFramework-Reverse-POCO-Code-First-Generator which generates mapping below:

HasRequired(a => a.table1)
    .WithMany(b => b.table2)
    .HasForeignKey(c => c.table1Code);

And when I try to use it I see that generated SQL code compares table1Id with table1Code (I think that it's because of table1Id as foreign key usage.)

Is it possible to create such mapping between those two tables, which are connected by table1Code?

ChW
  • 3,168
  • 2
  • 21
  • 34
Krzysiek
  • 55
  • 1
  • 8

0 Answers0