我当前正在尝试使用Visual Studio 2019创建ASP.NET Core Web应用程序。该应用程序的目的是显示从我导入的localdb数据库访问的所有数据。应用程序应将所有信息正确地输出到表中,并且用户应能够创建新记录,编辑,删除并还可以在不同屏幕上查看单个记录的详细信息。
尝试启动应用程序时,我一直遇到的错误如下:
SqlException:无效的列名'EmployeeID1'。
以下屏幕截图是我尝试从中提取数据的数据库结构:
此屏幕快照是文件结构:
这是我试图在表格中显示信息的方式:
启动应用程序时显示的错误如下:
SqlException:无效的列名'EmployeeID1'。
System.Data.SqlClient.SqlCommand + <> c.b__122_0(任务结果)System.Threading.Tasks.ContinuationResultTaskFromResultTask.InnerInvoke()System.Threading.ExecutionContext.RunInternal(ExecutionContext executeContext,ContextCallback回调,对象状态)System.Threading.Tasks.Task.ExecuteWithThreadLocal(参考Task currentTaskSlot)Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteAsync(IRelationalConnection连接,DbCommandMethod executeMethod,IReadOnlyDictionary参数值,CancellationToken cancelledToken)Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable + AsyncEnumerator.BufferlessMoveNext(DbContext ,bool buffer,CancellationToken cancelledToken)Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync(TState状态,Func>操作,Func >> verifySucceeded,CancellationToken cancelledToken)Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable + AsyncEnumerator.MoveNext(CancellationToken cancellingToken)System.Linq.AsyncEnumerable + SelectEnumerableAsyncIterator.MoveNextCore(CancellationToken cancelledToken)System.Linq.AsyncEnumerable + AsyncIterator.MoveNext(CancellationToken cancelledToken)Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider + ExceptionInterceptor + EnumeratorExceptionInterceptor.MoveNext(CancellationToken cancellingToken)System.Linq.AsyncEnumerable.Aggregate(IAsyncEnumerable源,TAccumulate种子,Func累加器,Func resultSelector,CancellationToken cancelledToken)Index.cshtml.cs中的WebApp2.IndexModel.OnGetAsync()-
//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();
}
}
}
我只是这个新手,对于为什么会发生这种情况并没有太多的了解,因此不胜感激。关于这个问题,有一些关于stackoverflow的答案,但是从答案中我不确定如何针对自己的情况进行调整。我希望我提供了足够的细节来说明为什么会发生这种情况。如果没有,请询问更多信息,我可以告诉您。谢谢。
公开的ICollection员工{组; }
您需要将ReportsTo
配置为Manager Navigation属性的外键属性。 EG
public int ReportsTo { get; set; }
[ForeignKey(nameof(Employee.ReportsTo))]
public virtual Employee Manager {get;set;}
public ICollection<Employee> Employees { get; set; }