How to map entities with inheritance - Entity Framework Core 2.0

.net-core c# entity-framework-core

Question

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

My entity which I want to map in [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]:

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]:

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]:

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]:

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]:

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.

I do it in .net full framework and it works here is the .net full framework code

1
2
9/19/2017 4:53:11 AM

Accepted Answer

By convention EF Core discovers and maps all properties of the entity class and all its base classes. In your case, Notifications property is discovered and identified as collection navigation property, hence the element type Notification is mapped as entity.

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:

modelBuilder.Ignore<Notification>();

References:

3
9/19/2017 10:20:13 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow