Entity Framework Is it possible to add an ASSOCIATION between Primary Keys and a Foreign Key

我的梦境 提交于 2019-12-01 05:22:22

In EF (3.5 and 4.0) FKs MUST point to Primary Keys.

But you appear to be attempting to point to a Candidate Key (i.e. [Countries].[CountryId]

I know that this is something the EF team are considering for the next version though :)

Hope this helps

Alex

For proper DB normalization, first thing is that primary keys must be only CountryId and StateId fields - the main Id fields for each table.

And ss I see from the description Name & IsoCode and Name & CountryId should be actually Unique keys, not primary.

Then the model class State should have a field:

public Country Country { get; set; }

Now EF have very good examples and since 4.3.1 + it fully supports Code first / DB first models, which I think will ease solving this. EF 5 have more compatibility updates so I think it wont be a problem for legacy DB engines.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!