【问题标题】:Error on migrating from EF Core 5 to EF Core 6从 EF Core 5 迁移到 EF Core 6 时出错
【发布时间】: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


    【解决方案1】:

    这看起来像一个错误。所以它应该作为一个问题提出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 中。所以这里也不需要。

    【讨论】:

    • 感谢您的帮助 :) 已报告错误。
    猜你喜欢
    • 2018-10-24
    • 2021-11-23
    • 2021-10-17
    • 2018-08-15
    • 2022-07-15
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多