I am trying to use code first with an existing db. So far it went well, but now I am failing at a one to many to one relationship.
There is a table customer and a table address in the db where address does NOT have any customerid but a foreign one to many key FK_Address_Customer.
The auto created classes from the edmx look like
Customer:
public int CustomerID (PK)
public Address Address
Address:
public int AddressID (PK)
public HashSet<Customer> Customers
Whatever I do in the fluent API either fails with invalid column Address_AddressID or Multiplicity conflicts with the referential constraint error.
I assumed:
//Customer has one address, address can have multiple customers<br/>
pModelBuilder.Entity<Customer>().HasRequired(m => m.Address).WithMany(x => x.Customers);
//Address has multiple customers, customer has one address<br/>
pModelBuilder.Entity<Address>().HasMany(m => m.Customers).WithRequired();
I got this correct on other tables where there where NO foreign keys in the db by using HasForeignKey, but in the above scenario it does not work. I also tried via MapKey passing the foreign key name also with no luck.
Ho do I get this simple relationship going ?
Thanks