SqlException: 无效的列名 'EmployeeID1'

SqlException: Invalid column name 'EmployeeID1'

我目前正在尝试使用 Visual Studio 2019 创建一个 ASP.NET 核心 Web 应用程序。该应用程序的目的是显示从我导入的 localdb 数据库访问的所有数据。应用程序应将所有信息正确输出到 table 中,并且用户应该能够创建新记录、编辑、删除以及在不同屏幕上查看单个记录的详细信息。

我在尝试启动应用程序时不断遇到的错误如下:

SqlException: Invalid column name 'EmployeeID1'.

以下屏幕截图是我尝试从中提取数据的数据库结构:

Database Structure

此截图为文件结构:

File Structure

这就是我试图在 table:

中显示信息的方式

Code

启动应用程序时显示的错误如下:

SqlException: Invalid column name 'EmployeeID1'.

System.Data.SqlClient.SqlCommand+ <>c.b__122_0(Task result)
System.Threading.Tasks.ContinuationResultTaskFromResultTask.InnerInvoke()
System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, object state)
System.Threading.Tasks.Task.ExecuteWithThreadLocal(ref Task currentTaskSlot)
Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteAsync(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary parameterValues, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable+AsyncEnumerator.BufferlessMoveNext(DbContext , bool buffer, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync(TState state, Func> operation, Func>> verifySucceeded, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable+AsyncEnumerator.MoveNext(CancellationToken cancellationToken) System.Linq.AsyncEnumerable+SelectEnumerableAsyncIterator.MoveNextCore(CancellationToken cancellationToken) System.Linq.AsyncEnumerable+AsyncIterator.MoveNext(CancellationToken cancellationToken) Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider+ExceptionInterceptor+EnumeratorExceptionInterceptor.MoveNext(CancellationToken cancellationToken) System.Linq.AsyncEnumerable.Aggregate
(IAsyncEnumerable source, TAccumulate seed, Func accumulator, Func resultSelector, CancellationToken cancellationToken) WebApp2.IndexModel.OnGetAsync() in Index.cshtml.cs -

        //public IList<Employee> Employee { get; set; }
        public IList<Employee> Employees { get;set; }

        public async Task OnGetAsync()
        {
            Employees = await _context.Employees.ToListAsync();
        }
    }
}
Microsoft.AspNetCore.Mvc.RazorPages.Internal.ExecutorFactory+NonGenericTaskHandlerMethod.Execute(object receiver, object[] arguments)
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.InvokeHandlerMethodAsync()
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.InvokeNextPageFilterAsync()
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.Rethrow(PageHandlerExecutedContext context)
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.InvokeInnerFilterAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

它在整个过程中向我传达的主要信息是错误位于以下代码中:

 Employees = await _context.Employees.ToListAsync();

以下代码是我在 Models 文件夹中的 Employee.cs 文件的代码:

using System;
using System.Collections.Generic;

namespace WebApp2.Models
{
    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 string PhotoPath { get; set; }

        public ICollection<Employee> Employees { get; set; }
    }
}

下一个代码是我目前的 index.cshtml.cs 文件。错误来自最后一行代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using WebApp2.Models;

namespace WebApp2
{
    public class IndexModel : PageModel
    {
        private readonly WebApp2.Models.Northwind _context;

        public IndexModel(WebApp2.Models.Northwind context)
        {
            _context = context;
        }

        //public IList<Employee> Employee { get; set; }
        public IList<Employee> Employees { get;set; }

        public async Task OnGetAsync()
        {
            Employees = await _context.Employees.ToListAsync();
        }
    }
}

我只是个新手,不太了解为什么会发生这种情况,因此非常感谢您的帮助。关于这个问题,Whosebug 上有一些答案,但从答案中我不完全确定如何根据我的情况调整它。我希望我已经提供了足够的细节来说明为什么会发生这种情况。如果没有,请询​​问更多信息,我可以告诉您。谢谢。

public ICollection Employees { get; set; }

您需要将 ReportsTo 配置为管理器导航 属性 的外键 属性。 EG

 public int ReportsTo { get; set; }

 [ForeignKey(nameof(Employee.ReportsTo))]
 public virtual Employee Manager {get;set;}

 public ICollection<Employee> Employees { get; set; }