[英].NET Core - Adding Scaffolded Item “There was an Error creating the DbContext to get the model”
每当我尝试为我的项目创建一个新的脚手架项目时,我收到一个错误说明
创建DbContect实例以获取为此对象定义的模型No Parameterless构造函数时出错。
我曾尝试重建我的项目,并且我之前已成功生成脚手架,但出于某种原因,现在这样做我没有取得任何成功。 自从我上次添加一个新的脚手架项目以来,我的上下文类没有进行任何更改。
我试图在我的模型中创建一个空构造函数,但它仍然会抛出相同的错误。 下面你会找到我的模型的代码,我正在尝试支架,以及我的上下文。
namespace HRIAT.Models
{
public class Credential
{
public Credential() { }
public int ID { get; set; }
public string credentialName { get; set; }
public FundingSource credentialFundingSource { get; set; }
public string credentialType { get; set; }
public float credentialCost { get; set; }
public ICollection<Employee> ec { get; set; }
}
}
上下文类:
namespace HRIAT.Models
{
public class HRIATContext : DbContext
{
public HRIATContext(DbContextOptions<HRIATContext> options)
: base(options)
{
}
public DbSet<FundingSource> FundingSource { get; set; }
public DbSet<Credential> Credential { get; set; }
public DbSet<Employee> Employee { get; set; }
public DbSet<EmployeesCredentials> EmployeeCredential { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<EmployeesCredentials>().HasKey(c => new { c.CredentialID, c.employeeNum });
}
public DbSet<Position> Positions { get; set; }
public DbSet<Department> Departments { get; set; }
public DbSet<Location> Locations { get; set; }
}
}
如果项目的实体不包含空构造函数,则会出现此错误。 EntityFramework需要空构造函数来实例化实体并映射模式的属性。
如果需要保护实体类,请使用受保护的构造函数,例如:
public class Person
{
public string Name { get; private set; }
public Person(string name)
{
Name = name;
}
protected Person() { }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.