[英]EFCore generating Column that is not valid
When i try to access the employees table for the Northwind database i get an invalid column name error, this column does not exist in the database and i am not sure how it is created or how to prevent it. 当我尝试访问Northwind数据库的employees表时,我得到一个无效的列名错误,该列在数据库中不存在,我不确定它是如何创建的或如何防止它。 i tried not to map the generated name but it just increments it 我试图不映射生成的名称,但它只是增加它
Error: 错误:
System.Data.SqlClient.SqlException (0x80131904): Invalid column name 'ManagerEmployeeID2'. System.Data.SqlClient.SqlException(0x80131904):列名称'ManagerEmployeeID2'无效。 at System.Data.SqlClient.SqlCommand.<>c.b__122_0(Task`1 result) 在System.Data.SqlClient.SqlCommand。<> c.b__122_0(任务`1结果)
Employees model class: 员工模型类:
namespace UWofS.CS7
{
public class Employee
{
public int EmployeeID { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string Title { get; set; }
public string TitleOfCourtesy { get; set; }
public DateTime? BirthDate { get; set; }
public DateTime? HireDate { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string Region { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public string HomePhone { get; set; }
public string Extension { get; set; }
public string Notes { get; set; }
public int ReportsTo { get; set; }
public Employee Manager { get; set; }
//public ICollection<Order> Orders { get; set; }
[NotMapped]
public Object ManagerEmployeeID { get; set; }
[NotMapped]
public Object ManagerEmployeeID1 { get; set; }
}
}
Index.cshtml.cs Index.cshtml.cs
namespace WebApp1.Pages.Employees
{
public class IndexModel : PageModel
{
private readonly UWofS.CS7.Northwind _context;
public IndexModel(UWofS.CS7.Northwind context)
{
_context = context;
}
public IList<Employee> Employee { get;set; }
public async Task OnGetAsync()
{
try
{
Employee = await _context.Employees.ToListAsync();
}
catch(Exception ex)
{
Console.Write(ex);
if (System.Diagnostics.Debugger.IsAttached == false)
{
System.Diagnostics.Debugger.Launch();
}
//System.Environment.Exit(13);
}
}
}
}
In Northwind Employees table "ReportsTo" column is the FK (self-referencing constraint) to establish an employee-manager relationship. 在Northwind Employees表中,“ReportsTo”列是用于建立员工 - 经理关系的FK(自引用约束)。 So your DbContext OnModelCreation method's Employee entity mapping will look like this: 所以你的DbContext OnModelCreation方法的Employee实体映射将如下所示:
modelBuilder.Entity<Employee>()
.HasOne(x => x.Manager)
.WithMany()
.HasForeignKey(x => x.ReportsTo);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.