2017-09-19 7 views
2

私は.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のフルフレームワークコード

答えて

3

すべてのプロパティエンティティクラスのそのすべての基底クラスを検出し、マップされています。あなたのケースでは、Notificationsプロパティが発見され、コレクションナビゲーションプロパティと識別されます。したがって、要素タイプNotificationはエンティティとしてマップされます。

デフォルトの仮定は、エンティティモデルがストアモデルを表すということです。非ストア属性を表すメンバは、明示的にマップされていない必要があります。

modelBuilder.Ignore<Notification>(); 

参考文献:

問題を解決するには、ちょうどあなたの OnModelCreatingオーバーライドに以下を追加します。
関連する問題