首页 新闻 赞助 找找看

升级至 .net core 3.0 遇到的 ef core 一对一映射关系配置问题

0
悬赏园豆:30 [已解决问题] 解决于 2019-04-27 13:55

之前用的是 .net core 2.2 ,一对一映射关系是这么配置的

builder.OwnsOne(post => post.ViewCount).HasForeignKey(vc => vc.EntryId);

升级至 .net core 3.0 之后,不支持 OwnsOne 之后直接跟 HasForeignKey ,必须要加 WithOwner ,于是改为:

builder.OwnsOne(post => post.ViewCount).WithOwner().HasForeignKey(vc => vc.EntryId);

但运行报错

The type 'ViewCount' cannot be configured as non-owned because an owned entity type with the same name already exists

请问如何解决这个问题?

dudu的主页 dudu | 高人七级 | 园豆:31075
提问于:2019-04-27 10:41
< >
分享
最佳答案
1

问题是 modelBuilder.Entity<ViewCount>().HasKey(v => v.EntryID)builder.OwnsOne(post => post.ViewCount).WithOwner().HasForeignKey(vc => vc.EntryId) 配置冲突引起的,被 own 的实体配置都要放在 OwnsOne 中,正确的配置方法如下:

builder.OwnsOne(post => post.ViewCount, vc => 
{
    vc.HasKey(v => v.EntryID);
    vc.WithOwner().HasForeignKey(_ => _.EntryID);
});
dudu | 高人七级 |园豆:31075 | 2019-04-27 13:54
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册