[英]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.