inheritance - EF Core 继承的属性未反射(reflect)在迁移脚本中

标签 inheritance ef-code-first entity-framework-core

我正在尝试迁移以下模型,并且继承未反射(reflect)在生成的迁移脚本中。我缺少什么?我目前正在使用 PM 来处理迁移脚本生成,在针对 SQL 2016 的 VS2017 中通过简单的 Add-Migration 和 Update-Database 来处理。

public class Facility
    {
        [Key]
        public int ID { get; set; }
        public bool Deleted { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public byte? Image { get; set; }

        public List<LocationFacility> LocationFacilities { get; set; }
    }

    public class Helipad : Facility
    {
        public decimal Size { get; set; }
        public decimal MaximumWeight {get; set;}
    }

最佳答案

基于Documentation of EF Core ,

为了按照约定将某种类型包含在模型中,您需要指向该类型的 DbSet 属性或 Navigation 属性,或者在 OnModelCreating 中引用它。 Facility 类上有导航属性。也许某些指向模型中包含的类型上的 Facility 的反向导航会导致包含 Facility。但除非您明确引用,否则不会添加 Helipad。 EF Core 为模型中包含且具有层次结构的类型设置 TPH。但如果不包含的话,它不会尝试在层次结构中查找类型。

要解决此问题,最简单的方法是为要包含在 DbContext 的派生类中的类型添加 DbSet 属性。如果您不想公开该类型的 DbSet 属性,则可以在 OnModelCreating 方法中编写以下内容。

modelBuilder.Entity<Helipad>();

希望这能解决问题。

关于inheritance - EF Core 继承的属性未反射(reflect)在迁移脚本中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45680187/

相关文章:

java - protected 抽象方法在父类包装实例的子类中不可见

json - .Net Core 3 和 EF Core 3 包含问题 (JsonException)

c# - 如何使 EF 核心将自定义排序转换为纯 'When Then' 而不是分层

c# - EF Core SaveChanges 是否根据数据注释进行验证

java - 我应该@Deprecate 父类(super class)方法吗?

C# 简化并可能概括了我的对象克隆方法

inheritance - 返回 F# 中的派生类

entity-framework-4 - Entity Framework 4.1 代码优先 : Single column foreign-key to multiple entities

entity-framework-4.1 - 使用自身具有外键/导航键的模型类

Mysql Entity Framework 问题 - 指定的键太长;最大 key 长度为 3072 字节