2017-09-19 1 views
2

.net 2.0 프로젝트가 있고 Entity Framework Core 2.0을 사용하고 있습니다. 상속이있는 엔터티를 매핑하려고하는데이 상속이 상속되었습니다.상속을 사용하여 엔터티를 매핑하는 방법 - Entity Framework 2.0 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...] 
} 

BaseEntity 클래스 [Domain.Project]의 :

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

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

Notifiable 클래스 [Shared.Project]에서 (보면, 그것은 Notification 유형의 목록을 가지고) :

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

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

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

Notification 클래스 [Shared.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; } 
} 
[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(); 
    } 
} 

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 클래스와 어느 것도 Notifiable 클래스를, 그들은 매핑되지 않아야합니다 .

나는 그물 전체 프레임 워크에서 할 그것은 here는 EF 코어가 발견 관례 그물 전체 프레임 워크 코드

답변

3

하고 엔티티 클래스의 모든 속성모든 기본 클래스 매핑 작동합니다. 귀하의 경우 Notifications 속성이 발견되고 컬렉션 탐색 속성으로 식별되므로 요소 유형 Notification이 엔티티로 매핑됩니다.

기본 가정은 엔터티 모델이 상점 모델을 나타내는 것이기 때문입니다. 비 상점 속성을 나타내는 멤버는 명시 적으로 매핑 해제되어야합니다. 이 문제를 해결하려면, 당신의 OnModelCreating 재정에 다음을 추가

modelBuilder.Ignore<Notification>(); 

참고 자료 :

관련 문제