Barry
Barry

Reputation: 372

Entity Framework 6 specific column mapping

In SQL, I would tell the database what the foreign key constraint is.

But fluent EF6 apparently does not have a way for me to specify what column to use when binding collections.

Is it not possible to tell DbModelBuilder exactly what column to bind relationships on? Or does it demand to be the primary key at all times?

Table_Person id int // pkey. Multiple people records UniqueID int // the unique person sometext varchar(256) // database therefore tracks changes to this, since unique person can have many records (pkeys).

Table_Address id int //pkey fk_unique int // should map to UniqueID of person, NOT the pkey. line1 varchar(512) state varchar(64) etc One unique person has many records, and their uniqueID (not pkey) has many associated addresses. Actual structure is far more complex than that. But am looking for a way to do this fundamentally...

Would very much so like to have an ICollection<Address> Addresses within the Persons model. But to enable such a thing for code-first migrations... seems impossible? Yes I could Add-Migration and then modify the generated code/sql manually. But doesn't that defeat the point? Or is that common practice?

Upvotes: 0

Views: 371

Answers (1)

Brandon
Brandon

Reputation: 1664

If you're able to modify the DB schema you could put UniqueIDs for people into their own table named "Person" and rename the existing table to "PersonVersion". Then have FKs to the new "Person" table on "PersonVersion" and "Address". And finally, create the Person, PersonVersion, and Address models in your app code and EF should bind without problem.

Upvotes: 1

Related Questions