我正在尝试使用 EF7 迁移,但在使用继承对Organization
模型进行建模时卡住了。
Organization
是一个抽象类。有两个具体的类继承自它,称为 Individual
和 Company
。
我将Organization
抽象类设置为 DbContext
中的DbSet<Organization>
并运行迁移。
我在这里遵循本教程。
将显示以下错误:
实体类型"组织"的相应 CLR 类型不可实例化,并且模型中没有与具体 CLR 类型对应的派生实体类型。
我该怎么办?
编辑 - 使用代码更新。
组织:
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; }
}
个人
public class Individual : Organization
{
public string SocialSecurityNumber { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
}
公司
public class Company : Organization
{
public string Name { get; set; }
public string OrganizationNumber { get; set; }
}
数据库上下文
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);
}
}
提前感谢!
请参阅:https://learn.microsoft.com/en-us/ef/core/modeling/inheritance
如果不想为层次结构中的一个或多个实体公开 DbSet,则可以使用 Fluent API 来确保它们包含在模型中。
如果您不想为每个子类创建一个DbSet
,则必须在DbContext
的OnModelCreating
覆盖中显式定义它们:
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);
}
}
万一有人像我一样犯了一个愚蠢的错误,我的实体是抽象的。因此,请检查您是否错误地提出了相同的问题。
与您链接的教程类似,您的DbSet<>
属性应该是继承Individual
和Company
类。
尝试让您的CoreDbContext
看起来更像这样:
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);
}
}
问题可能是您使用class library
项目。在 EF Core 的 RC2 中,不能将 DBContext 放在此类项目中。这是一个已知问题。当您将其转换为"应用程序"项目时,它应该会再次工作。
更多信息和来源:
解决办法:https://docs.efproject.net/en/latest/cli/dotnet.html#dotnet-cli-issues
Github问题:https://github.com/aspnet/EntityFramework/issues/5460
您还应该将继承 Organization 的类放入CoreDbContext
类中。然后,它将正确区分并实例化这些对象。它应该看起来像这样:
public DbSet<Organization> Organization { get; set; }
public DbSet<Individual> Individual { get; set; }
public DbSet<Company> Company { get; set; }
来源: https://learn.microsoft.com/en-us/ef/core/modeling/inheritance