在从EF Core 5迁移到EF Core 6之后,我的db模型出现了问题。
在上下文创建中,我得到了错误:给定的键在字典中不存在。
应用程序回购示例:https://github.com/testApp6/TestApp
有什么不对的地方吗?或者我怎么解决这个问题?
发布于 2021-12-10 16:15:10
这看起来像个窃听器。因此,它应该作为一个问题提出这里。
处理属性的反射代码被显式接口实现弄糊涂了:
[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。所以这里也没必要这么做。
https://stackoverflow.com/questions/70307206
复制相似问题