我尝试使用EF7迁移,并在Organization
使用继承对模型进行建模时陷入困境。
Organization
是一个抽象类。有两个具体的类从Individual
和继承而来Company
。
我将Organization
抽象类设置为DbSet<Organization>
inDbContext
并运行迁移。
我在这里关注本教程。
显示以下错误:
实体类型“组织”的对应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; }
}
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);
}
}
提前致谢!
请参阅:https : //docs.microsoft.com/en-us/ef/core/modeling/inheritance
如果不想为层次结构中的一个或多个实体公开DbSet,则可以使用Fluent API确保它们包含在模型中。
如果您不想DbSet
为每个子类创建一个,则必须在的OnModelCreating
覆盖中显式定义它们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);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句