私は.netコア2.0プロジェクトを持っており、Entity Frameworkコア2.0を使用しています 継承を持つエンティティをマップしたいが、この継承にも継承があります継承を持つエンティティのマップ方法 - Entity Frameworkコア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]
でNotifiable
クラス(それが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
クラス:[Infra.Project]
で
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; }
}
マイ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();
}
}
私のMapping
クラス[Infra.Project]
:
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
クラスとどちらNotifiable
クラスでもないが、私のコンテキストにマッピングされている、彼らは、マップされてはいけません。
私は、.NETの完全なフレームワークでそれを行う、それがhereを作品慣例EFコアによって、.NETのフルフレームワークコード