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

后端 未结 2 1551
盖世英雄少女心
盖世英雄少女心 2021-01-13 00:55

I\'ve got the following entities on my EDMX :- \"alt

These two entites were generated by Update Mode

相关标签:
2条回答
  • 2021-01-13 00:59

    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.

    0 讨论(0)
  • 2021-01-13 01:04

    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

    0 讨论(0)
提交回复
热议问题