首页 新闻 会员 周边 捐助

使用 EF Core 时如何将两个不同的实体映射到同一张表

0
悬赏园豆:30 [已解决问题] 解决于 2025-05-13 17:01

Link 实体与 PostToCategory 实体,如果直接通过 builder.ToTable("blog_Links") 映射到同一张表会报下面的错误

System.InvalidOperationException: Cannot use table 'blog_Links' for entity type 'Link' since it is being used for entity type 'PostToCategory' and potentially other entity types, but there is no linking relationship. Add a foreign key to 'Link' on the primary key properties and pointing to the primary key on another entity type mapped to 'blog_Links'.
dudu的主页 dudu | 高人七级 | 园豆:24691
提问于:2025-05-12 22:44
< >
分享
最佳答案
0

通过一个变通方法解决了,在 SQL Server 数据库中添加一个指向 blog_Links 的同义词(synonym)欺骗一下 EF Core,其中一个实体映射到这个 synonym

dudu | 高人七级 |园豆:24691 | 2025-05-13 17:00
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册