Sto cercando di utilizzare le migrazioni EF7 e mi sono bloccato quando ho modellato un modello di Organization
con ereditarietà.
Organization
è una classe astratta. Esistono due classi concrete che ereditano da essa denominate Individual
e Company
.
Ho impostato la classe astratta Organization
come DbSet<Organization>
in DbContext
ed DbContext
migrazioni.
Sto seguendo questo tutorial qui .
Viene visualizzato il seguente errore:
Il tipo CLR corrispondente per il tipo di entità "Organizzazione" non è istantaneo e nel modello non esiste un tipo di entità derivata che corrisponda a un tipo CLR concreto.
Cosa dovrei fare
EDIT: aggiornato con il codice.
Organizzazione:
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; }
}
Individuale
public class Individual : Organization
{
public string SocialSecurityNumber { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
}
Azienda
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);
}
}
Grazie in anticipo!
Si prega di consultare: https://docs.microsoft.com/en-us/ef/core/modeling/inheritance
Se non si desidera esporre un DbSet per una o più entità nella gerarchia, è possibile utilizzare l'API Fluent per assicurarsi che siano incluse nel modello.
Se non si desidera creare un DbSet
per ciascuna sottoclasse, è necessario definirli esplicitamente OnModelCreating
di 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)
{
builder.Entity<Individual>();
builder.Entity<Company>();
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);
}
}
Simile al tutorial che hai collegato, le proprietà DbSet<>
dovrebbero essere le classi Individual
e Company
ereditano.
Prova ad avere il tuo CoreDbContext
più simile a questo:
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);
}
}