Association of Entity Framework with Keyless Fields

Is it possible to create associated b / t 2 elements of non-integrated fields in the Entity Framework?

Example. Take 2 tables in an outdated application (i.e. keys / structure cannot change)

Order ( OrderId : int : PK OrderNo : varchar ) OrderDetails ( DetailRecordId : int : PK OrderNo : varchar ) 

In the Entity Framework, I want to create a b / t Order and OrderDetails association in the OrderNo field, which is not the primary key in any table or FK relationships in the database.

It seems to me that this should not only be easy to do, but also one reason to use something like EF. However, it seems that I only want to create associations using entity keys.

+6
entity-framework
source share
3 answers

Entity Framework allows you to claim that columns are keys, and FK restrictions exist where the database does not actually exist.

This is due to the fact that the SSDL (part of StorageModel EDMX) can, if necessary, be manipulated by you and lie about the database.

EF then interacts with the database as if the keys and foreign keys really exist.

This should work, but all normal referential integrity clauses apply.

See my Entity Framework Tips

Hope this helps.

+6
source share

The problem with using non-key fields to define relationships is that keys are not guaranteed properly. This can lead to a situation where you have a one-to-one relationship between two objects, where there are several possible lines that complement the relationship.

... when binding data from a database, relationships should always be based on keys. Keys provide link integrity.

+1
source share

Another workaround:

create a vOrder view that will not include PK and create Entity from it. Set PK in this object to OrderNo

Now you can create associations

0
source share

All Articles