我的错误:实体类型为'Employee'的相应CLR类型无法实例化,并且没有模型中与具体CLR类型相对应的派生实体类型我正在尝试在ef core中使用迁移。 (我正在使用代码优先方法)
我有一个抽象类名称Employee,它具有ID,名称,用户ID等属性
现在我的要求是指定雇员是合同工还是永久工
P.S,我是应届生,不知道我在这里做错了什么
我的代码如下
public abstract class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string UserId { get; set; }
[ForeignKey("UserId")]
public virtual ApplicationUser User { get; set; }
}
My concrete class :
public class Contract : Employee
{
// I just left it as empty because I don't want any other columns to add
}
public class Permanent : Employee
{
// Here too I just left it as empty because I don't want any other columns to add
}
and my DbContext like :
public class CoreDbContext : IdentityDbContext<ApplicationUser>
{
public CoreDbContext(DbContextOptions<CoreDbContext> options)
: base(options)
{
}
public DbSet<Employee> Employees { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Contract>();
builder.Entity<Permanent>();
base.OnModelCreating(builder);
}
}
Correct me where I am wrong.
您将需要定义类Employee
到表的映射,如下所示:
builder.Entity<Employee>().ToTable("Employee");
这将使用每个类的表层次结构/单表层次结构映射模型,该模型使用一列作为存储在同一表上的不同类型之间的区分符。