c# - 如何通过继承映射实体 - Entity Framework Core 2.0

标签 c# .net-core entity-framework-core

我有一个 .net core 2.0 项目,并且正在使用 Entity Framework Core 2.0 我想映射一个具有继承的实体,并且该继承也具有继承

我想要在[Domain.Project]中映射的实体:

public class Customer : BaseEntity
{
    public Customer(Name name, DateTime? birthDay, Email email, string password, List<CreditDebitCard>  creditDebitCards = null)
    {
            CreationDate = DateTime.Now;
            Name = name;
            BirthDay = birthDay;
            Email = email;
            Password = password;
            _CreditDebitCards = creditDebitCards ?? new List<CreditDebitCard>();
    }

    [Fields...]
    [Properties...]
    [Methods...]
}

[Domain.Project] 中我的 BaseEntity 类:

public abstract class BaseEntity : Notifiable
{
    public BaseEntity()
    {
            CreationDate = DateTime.Now;
            IsActive = true;
    }

    [Fields...]
    [Properties...]
    [Methods...]
}

[Shared.Project] 中我的 Notifying 类(看,它有一个 Notification 类型列表):

public abstract class Notifiable
{
    private readonly List<Notification> _notifications;

    protected Notifiable() { _notifications = new List<Notification>(); }

    public IReadOnlyCollection<Notification> Notifications => _notifications;
    [Methods...]
}

[Shared.Project] 中我的 Notification 类:

public class Notification
{
    public Notification(string property, string message)
    {
        Property = property;
        Message = message;
    }

    public string Property { get; private set; }
    public string Message { get; private set; }
}

[Infra.Project] 中的我的 Entity Framework 上下文类:

public class MoFomeDataContext : DbContext
{
    public DbSet<Customer> Customers { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(Runtime.ConnectionString);
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<CreditDebitCard>().Map();
    }
}

[Infra.Project] 中我的 Mapping 类:

public static class CustomerMap
{ 
    public static EntityTypeBuilder<Customer> Map(this EntityTypeBuilder<Customer> cfg)
    {
        cfg.ToTable("Customer");
        cfg.HasKey(x => x.Id);
        cfg.Property(x => x.BirthDay).IsRequired();
        cfg.OwnsOne(x => x.Email);
        cfg.OwnsOne(x => x.Name);
        cfg.HasMany(x => x.CreditDebitCards);

        return cfg;
    }
}

当我尝试添加迁移时,出现此错误:

The entity type 'Notification' requires a primary key to be defined.

但是 Notification 类和 Notifying 类都没有在我的上下文中映射,它们不能被映射。

我在.net完整框架中完成它并且它有效here是.net完整框架代码

最佳答案

按照惯例,EF Core 会发现并映射实体类及其所有基类所有属性。在您的情况下,Notifications 属性被发现并标识为集合导航属性,因此元素类型 Notification 被映射为实体。

这是因为默认假设是实体模型代表一个商店模型。表示非存储属性的成员应显式取消映射。要解决此问题,只需将以下内容添加到您的 OnModelCreating 覆盖中:

modelBuilder.Ignore<Notification>();

引用文献:

关于c# - 如何通过继承映射实体 - Entity Framework Core 2.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46290086/

相关文章:

c# - 具有值数组的 NUnit 测试

c# - Linux 上的 Dotnet 核心段错误

c# - 在 Winforms .NET Core 中使用命令链接

dependency-injection - 当流畅的调度程序作业调用方法时,无法访问已处置的对象异常

c# - 实体类型 'Configuration' 需要定义主键

.net-core - EF Core 工具无法正确安装

c# - 遍历 TabControl 中的 TabItems

C# INSERT 与 INSTEAD OF INSERT 触发器

c# - 无法加载文件或程序集 Microsoft.Owin.Security.OAuth,版本 = 2.0.0.0

c# - 一对多返回空数组