【发布时间】:2022-01-15 07:19:18
【问题描述】:
从 EF Core 5 迁移到 EF Core 6 后,我的数据库模型出现问题。
在创建上下文时出现错误:字典中不存在给定的键。
示例应用程序仓库:https://github.com/testApp6/TestApp
有什么想法有什么问题或我该如何解决?
【问题讨论】:
标签: entity-framework-core ef-core-5.0 ef-core-6.0
从 EF Core 5 迁移到 EF Core 6 后,我的数据库模型出现问题。
在创建上下文时出现错误:字典中不存在给定的键。
示例应用程序仓库:https://github.com/testApp6/TestApp
有什么想法有什么问题或我该如何解决?
【问题讨论】:
标签: entity-framework-core ef-core-5.0 ef-core-6.0
这看起来像一个错误。所以它应该作为一个问题提出here。
处理你的属性的反射代码被你的显式接口实现弄糊涂了:
[ForeignKey(nameof(PostTypeEnum))]
[InverseProperty(nameof(Model.PostType.Posts))]
public PostType PostType { get; set; }
[NotMapped]
PostTypeCommon IPost.PostType => PostTypeEnum.ToCommon();
要解决这个问题,只需从 Post 中删除该属性:
[Table(nameof(PostType))]
public class PostType
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
public virtual PostTypeEnum Enum { get; set; }
[MaxLength(20), Required]
public virtual string Code { get; set; }
//[InverseProperty(nameof(Post.PostType))]
public virtual ICollection<Post> Posts { get; set; }
}
您已经在导航属性的另一侧配置了 InverseProperty,和在 OnModelCreating 中。所以这里也不需要。
【讨论】: