I need to have an entity that has two separate candidate keys where one of them is the primary key, which is a surrogate identity key, and another one that is an alternate key representing the real unique attribute. I need to have them both separately in my entity. Please note that I am not referring to composite or multi-column keys. Anyhow I need to have this on one of my entities in ADO.NET data model. Is it possible to do that? If yes please guide me.
The reason to have the second key is that I need another entity to have an association (and foreign key) relationship with first entity on the that key.
Update: I found a very similar situation to mine in the following question: http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/a248632a-d305-4c15-8e57-6742457cca94 It seems that EF v1 does not support this feature. Does anybody know if V4(literally the second version) has this feature or not? I have found the following but 开发者_运维技巧it does not seem to show any clues that this feature has been added to the current version: http://blogs.msdn.com/b/adonet/archive/2009/05/11/update-on-the-entity-framework-in-net-4-and-visual-studio-2010.aspx
Entity framework doesn't support unique keys. In entity model your second key will be field as any other. You will not be able to create relations on that key and EF will not check that value of the key is unique. But if your model will be based on existing database with unique keys you will have this check on database layer.
Support for unique keys has been hinted at by the EF team but remains unavailable as of .NET 4.5.
If you are using Code First and the fluent API, creating two seperate DbContexts might be a good solution for you. You can write separate EntityTypeConfiguration classes for each of the keys that you want to use in navigation properties. We ran into this issue on our project as well and I wrote a blog post describing our solution: http://mmilleruva.blogspot.com/2013/10/working-with-legacy-database-schemas.html
精彩评论