繁体   English   中英

EFCore生成无效的列

[英]EFCore generating Column that is not valid

当我尝试访问Northwind数据库的employees表时,我得到一个无效的列名错误,该列在数据库中不存在,我不确定它是如何创建的或如何防止它。 我试图不映射生成的名称,但它只是增加它

错误:

System.Data.SqlClient.SqlException(0x80131904):列名称'ManagerEmployeeID2'无效。 在System.Data.SqlClient.SqlCommand。<> c.b__122_0(任务`1结果)

员工模型类:

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

    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);
            }
        }
    }
}

在Northwind Employees表中,“ReportsTo”列是用于建立员工 - 经理关系的FK(自引用约束)。 所以你的DbContext OnModelCreation方法的Employee实体映射将如下所示:

modelBuilder.Entity<Employee>()
                .HasOne(x => x.Manager)
                .WithMany()
                .HasForeignKey(x => x.ReportsTo);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM