Rovdjuret Rovdjuret - 2 months ago 75
C# Question

EF7 Migrations - The corresponding CLR type for entity type '' is not instantiable

I'm trying to use EF7 migrations and got stuck when I modelled a Organization model with inheritance.

Organization is a abstract class and there is two concrete classes that inherit it called Individual and Company.

I set the Organization abstract class as DbSet in DbContext and run migrations.

I'm following this tutorial here

The following error is shown:

The corresponding CLR type for entity type 'Organization' is not instantiable and there is no derived entity type in the model that corresponds to a concrete CLR type.

Whats should I do?

EDIT - Updated with code

Organization

public abstract class Organization
{
public Organization()
{
ChildOrganizations = new HashSet<Organization>();
}

[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public bool Enabled { get; set; }
public bool PaymentNode { get; set; }
public DateTime Created { get; set; }
public DateTime Updated { get; set; }

// virtual
public virtual ICollection<Organization> ChildOrganizations { get; set; }

}


Individual

public class Individual : Organization
{
public string SocialSecurityNumber { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
}


Company

public class Company : Organization
{
public string Name { get; set; }
public string OrganizationNumber { get; set; }
}


DbContext

public class CoreDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<Organization> Organization { get; set; }

public CoreDbContext(DbContextOptions<CoreDbContext> options)
: base(options)
{
}

protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}
}


Thanks in advance!

Answer

Similar to the tutorial you linked, your DbSet<> properties should be the inheriting Individual and Companyclasses.

Try having your CoreDbContext look more like this:

public class CoreDbContext : IdentityDbContext<ApplicationUser>
{
    public DbSet<Company> Companies { get; set; }
    public DbSet<Individual> Individuals { get; set; }

    public CoreDbContext(DbContextOptions<CoreDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);
    }
}