简体   繁体   English

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

[英]How to map entities with inheritance - Entity Framework Core 2.0

I have a .net core 2.0 project and I'm using Entity Framework Core 2.0 I want to map an entity that has an inheritance and this inheritance has an inheritance too 我有一个.net core 2.0项目,正在使用Entity Framework Core 2.0,我想映射一个具有继承关系的实体,并且该继承关系也具有继承关系

My entity which I want to map in [Domain.Project] : 我想在[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...]
}

My BaseEntity class in [Domain.Project] : 我在[Domain.Project] BaseEntity类:

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

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

My Notifiable class (look, it have a list of Notification type) in [Shared.Project] : [Shared.Project]我的Notifiable类(看起来,它具有Notification类型的列表):

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

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

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

My Notification class in [Shared.Project] : [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; }
}

My Entity Framework context class in [Infra.Project] : 我在[Infra.Project]实体框架上下文类:

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();
    }
}

My Mapping class in [Infra.Project] : 我在[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;
    }
}

When I try add a migration, I get this error: 当我尝试添加迁移时,出现此错误:

The entity type 'Notification' requires a primary key to be defined. 实体类型“通知”要求定义主键。

But neither the Notification class and neither the Notifiable class have been mapped in my context, they must not be mapped. 但是在我的上下文中都没有映射Notification类和Notifiable类,因此不能映射它们。

I do it in .net full framework and it works here is the .net full framework code 我是在.net完整框架中完成的,在这里工作的是.net完整框架的代码

By convention EF Core discovers and maps all properties of the entity class and all its base classes . 按照约定,EF Core会发现并映射实体类及其所有基类的 所有属性 In your case, Notifications property is discovered and identified as collection navigation property , hence the element type Notification is mapped as entity. 在您的情况下, Notifications属性被发现并标识为集合导航属性 ,因此元素类型Notification被映射为实体。

It's because the default assumption is that the entity model represent a store model . 这是因为默认假设是实体模型代表商店模型 The members representing non store attributes should be unmapped explicitly. 代表非商店属性的成员应显式取消映射。 To fix the issue, just add the following to your OnModelCreating override: 要解决此问题,只需将以下内容添加到您的OnModelCreating覆盖中:

modelBuilder.Ignore<Notification>();

References: 参考文献:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Entity Framework Core 2.0为X实体创建链接的X实体 - How do I create linked X entities for a X entity using Entity Framework Core 2.0 与Entity Framework Core 2.0的多个子实体中的属性名称相同 - Same property name in several child entities with Entity Framework Core 2.0 如何使用Entity Framework Core加载相关实体 - How to load related entities with the Entity Framework Core 实体框架核心2.0功能如何工作 - Entity framework core 2.0 Functions how to work 如何在Entity Framework Core 2.0中映射值对象 - Howto map value object in Entity Framework Core 2.0 如何在实体框架中将一个实体映射到许多实体? - How do I map one entity to many entities in Entity Framework? Map 实体到带有实体框架的数据库 - Map entities to database with Entity Framework 如何使用身份和实体框架在.NET Core 2.0中管理身份验证和授权 - How to manage authentication and authroization in .NET Core 2.0 with Identity & Entity Framework Entity Framework Core 2.0中的迁移-如何指定环境或传递参数 - Migrations in Entity Framework Core 2.0 - How to specify environment or pass arguments 如何修复 Entity Framework Core 2.0 中的 n+1 问题? - How to fix n+1 problem in Entity Framework Core 2.0?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM