C# EF Scaffold DbContext反向工程表-生成无效列名错误
VS2019,核心3.1,C# 我从现有数据库反向迁移了一个表:C# EF Scaffold DbContext反向工程表-生成无效列名错误,c#,.net,sql-server,.net-core-3.1,ef-core-3.1,C#,.net,Sql Server,.net Core 3.1,Ef Core 3.1,VS2019,核心3.1,C# 我从现有数据库反向迁移了一个表: Scaffold DbContext“我的连接字符串”Microsoft.EntityFrameworkCore.SqlServer-OutputDir Models-Tables“Accounts” 其中一列是AcctSN(varchar(50),null)。它的生成如下所示: publicstringacctsn{get;set;} 看不出这有什么问题。(它不是键,上面没有索引。) 我把一切都连接好了,当我打电话的时候 var
Scaffold DbContext“我的连接字符串”Microsoft.EntityFrameworkCore.SqlServer-OutputDir Models-Tables“Accounts”
其中一列是AcctSN(varchar(50),null)。它的生成如下所示:
publicstringacctsn{get;set;}
看不出这有什么问题。(它不是键,上面没有索引。)
我把一切都连接好了,当我打电话的时候
var accounts=await dbContext.Account.ToListAsync()代码>
我得到以下错误:
“无效的列名'AcctSN'。”
下面是整个错误:
Class: 16
ClientConnectionId: {REDACTED GUID}
Data: {System.Collections.ListDictionaryInternal}
ErrorCode: -2146232060
Errors: {Microsoft.Data.SqlClient.SqlErrorCollection}
HResult: -2146232060
HelpLink: null
InnerException: null
LineNumber: 1
Message: "Invalid column name 'AcctSN'."
Number: 207
Procedure: ""
Server: "REDACTED"
Source: "Core Microsoft SqlClient Data Provider"
StackTrace: " at Microsoft.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__164_0(Task`1 result)\r\n at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()\r\n at System.Threading.Tasks.Task.<>c.<.cctor>b__274_0(Object obj)\r\n at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.Get
Result()\r\n at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.<ExecuteReaderAsync>d__17.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.<ExecuteReaderAsync>d__17.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.<ExecuteReaderAsync>d__17.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.<InitializeReaderAsync>d__18.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServi
ces.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.<ExecuteAsync>d__7`2.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.<MoveNextAsync>d__17.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.<ToListAsync>d__64`1.MoveNext()\r\n at System.Runtime.ExceptionService
s.ExceptionDispatchInfo.Throw()\r\n at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.<ToListAsync>d__64`1.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\r\n at REDACTED.AccountRepository.<GetAccounts>d__2.MoveNext() in REDACTED"
State: 1
TargetSite: {System.Data.Common.DbDataReader <ExecuteDbDataReaderAsync>b__164_0(System.Threading.Tasks.Task`1[Microsoft.Data.SqlClient.SqlDataReader])}
Class:16
ClientConnectionId:{REDACTED GUID}
数据:{System.Collections.ListDictionaryInternal}
错误代码:-2146232060
错误:{Microsoft.Data.SqlClient.SqlErrorCollection}
HResult:-2146232060
帮助链接:空
InnerException:null
行号:1
消息:“无效的列名'AcctSN'。”
电话:207
程序:“”
服务器:“已编辑”
来源:“核心Microsoft SqlClient数据提供程序”
StackTrace:“在Microsoft.Data.SqlClient.SqlCommand.c.b\uu 164\u 0(任务'1结果)\r\n在System.Threading.Tasks.ContinuationResultTaskFromResultTask'2.InnerInvoke()\r\n在System.Threading.Tasks.Task.c.b\uu 274\u 0(对象obj)\r\n在System.Threading.ExecutionContext.RunInternal(ExecutionContext ExecutionContext,ContextCallback callback,对象状态)\r\n在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n在System.Threading.ExecutionContext.RunInternal(ExecutionContext ExecutionContext,ContextCallback callback,对象状态)\r\n在System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task¤tTaskSlot,Thread threadPoolThread)\r\n在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\r\n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n在System.Runtime.CompilerServices.TaskAwaiter`1.Get
结果()\r\n在Microsoft.EntityFrameworkCore.Storage.RelationalCommand.d\u 17.MoveNext()\r\n在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n在Microsoft.EntityFrameworkCore.Storage.RelationalCommand.d\u 17.MoveNext()\r\n在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中\r\n在Microsoft.EntityFrameworkCore.Storage.RelationalCommand.d\u 17.MoveNext()\r\n在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n在System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务任务任务)\r\n在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务任务任务任务)\r\n在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n在Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.d\u 18.MoveNext()\r\n在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n在System.Runtime.CompilerService
ces.TaskAwaiter.ThrowForNonSuccess(Task Task)\r\n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task Task Task)\r\n在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n在Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.d_u7`2.MoveNext()\r\n在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务任务)\r\n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务任务任务)\r\n位于Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.d\u 17.MoveNext()\r\n位于System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n位于System.Runtime.CompilerServices.TaskWaiter.ThrowForOnSuccess(任务任务)\r\n位于Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.d\u 64`1.MoveNext()\r\n位于System.Runtime.ExceptionService
s、 ExceptionDispatchInfo.Throw()\r\n位于Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.d\u 64`1.MoveNext()\r\n位于System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n位于System.Runtime.CompilerServices.TaskWaiter.ThrowForOnSuccess(任务任务)\r\n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n在System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\r\n在编校的.AccountRepository.d_2.MoveNext()中
国家:1
TargetSite:{System.Data.Common.DbDataReader b__164_0(System.Threading.Tasks.Task`1[Microsoft.Data.SqlClient.SqlDataReader]))
这是一个独立的表。它与其他表没有关系。(我没有生成此数据库。)没有外键。该类具有另一个字符串属性,该属性成功链接到数据库中的varchar列;当我注释掉此AcctSN字段时,该属性起作用
任何想法。到目前为止,我发现的所有内容都涉及到密钥和关系的问题,但正如我上面所述,这不适用于这种情况。您在每种情况下都使用相同的连接字符串吗?是的。我看到另一篇帖子,海报不是这样,所以我检查了。当您将属性的名称更改为完全不同的名称时,它仍然错误吗?您的意思是使用数据注释吗?我已经尝试了[Column(“AcctSN”)]公共字符串AccountShortName{get;set;}
尝试重命名数据库中的列,然后再次scaffold。如果成功,它会告诉您te“AcctSN”作为一个名称是一个问题。如果不成功,则说明您的列有问题。在这两种情况下,如果您可以在另一个项目中重现该问题,您可以在GitHub上向EFCore团队提交一个错误。