Inheritance and composite foreign keys - one part of the key in base class, the other part in derived class

后端 未结 3 1004
轮回少年
轮回少年 2020-12-11 01:21

I am having problems to create an Entity Framework Code-First mapping for the following sample database schema (in SQL Server):

相关标签:
3条回答
  • 2020-12-11 01:31

    Well, I can't seem to comment on anything so I'm adding this as an answer.

    I created an Issue on CodePlex for this problem so hopefully they will look into it soon. Stay tuned!

    http://entityframework.codeplex.com/workitem/865


    Result of the Issue at CodePlex (which has been closed in the meantime) is that the scenario in the question is not supported and there are currently no plans to support it in the near future.

    Quote from the Entity Framework team at CodePlex:

    This is part of a more fundamental limitation where EF doesn't support having a property defined in a base type and then using it as a foreign key in a derived type. Unfortunately this is a limitation that would be very hard to remove from our code base. Given that we haven't seen a lot of requests for it, it's not something we are planning to address at this stage so we are closing this issue.

    0 讨论(0)
  • 2020-12-11 01:35

    I think it is simpler and reduces complexity to have Table --> TableId (PK) --> other columns including FKs.

    So in your example - adding a CustomerId column to the Customers table would solve your problem.

    0 讨论(0)
  • 2020-12-11 01:43

    Not a solution, but a workaround (*): a nice choice is to use single Id columns (as ), usually auto-incremented, and providing database integrity using foreign keys, unique indexes, etc. More complex data integrity could be achieved with triggers, so maybe you could be heading that way, but you might leave that to the application business logic level, unless the application is really data-focused. But since you're using Entity Framework, it's probably safe to assume that this is not your case...?

    (*) as suggested by ivowiblo

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