问题标签 [ef-core-2.2]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
637 浏览

.net-core - 当 EFCore 尝试将数据从存储库返回到控制器时,为什么 asp.net 核心项目会崩溃?

我正在尝试通过在我的 asp.net 核心 mvc 应用程序中使用 EFCore 从数据库中获取数据,但是当存储库尝试将数据返回到控制器时,应用程序崩溃了。

工具我正在使用 Vs2017、MSSQL2017、Asp.net Core 2.2、EFCore2.2。我已经创建了一个存储库,用于对数据库中的用户进行身份验证。当我尝试通过控制器发送用户名和密码来通过存储库对用户进行身份验证时,应用程序崩溃。

即使我在调试时可以看到该用户已通过身份验证,并且存储库从数据库中获取该用户。但是当存储库尝试将用户返回到控制器时,应用程序就会崩溃。它不会生成任何类型的异常,但是一旦存储库尝试返回用户就会崩溃。

但是我在Vs2017的输出窗口中可以看到一些信息,如下

“iisexpress.exe”已退出,代码为 -1073741819 (0xc0000005)“访问冲突”

控制器代码

公共类 LoginController : AppController { 私有只读 IUserRepository _userRepository;

存储库代码

public UserModel FindUser(string userName, string password) {

我希望在验证用户身份后应该移动到另一个页面。

0 投票
0 回答
130 浏览

c# - 是否有任何框架或组件可以在 EF Core/ASP.Net Core 中构建复杂类型?

使用 EF,我有很多类型:

脚手架功能非常适合构建简单的控制器和视图,但是,我一直在修改默认值以添加对众多复杂类型的支持,或者支持外键。

目前,我已经为自己构建了一些基本上会导致弹出并执行 AJAX 类型查询,然后填充字段的东西。它可以工作 - 但是,它不是好的代码,我对它不满意。

我一直在寻找年龄,除了旧的MVC 脚手架项目之外,我已经变成了一个空白- 我想知道是否有更好的脚手架模板或框架可以帮助解决这个问题,因为这显然是一项非常常见的任务,我不敢相信我一个人在这里需要更好的解决方案。

0 投票
0 回答
271 浏览

entity-framework-core - EF Core TPH Migration - Derived types have no key defined

EF migrations fails and tells me that the derived types have no key defined.

I'm trying to setup TPH, which should be fairly easy with attributes, but I can't get it to work with separate (fluent-)configuration files. It always reports that the key of the derived types is not defined.

If I define it on the derived types, then it says the key must be defined for the base type.

Entity type configurations:

Model classes:

I can't get rid of this error:

Migration error

Please help me and tell me what I am doing wrong. I can't seem to figure it out...

0 投票
1 回答
296 浏览

c# - UWP 6.2.2 and System.Runtime.CompilerServices.Unsafe Incompatibility

I am fairly new to the UWP platform, and I'm having a bit of trouble trying to create my app package. The release build works fine, but when I try to make an app package for side-loading I get the following error.

It does seem that there are some dependencies in my project that contain the same class, but I'm not sure how to fix the problem.

In my project I have the following installed.

I also have two supporting projects. One is a class library and the other is a migrations app. Combined they use the following.

As I said above I can see there seems to be a file conflict in these two libraries, but I'm just not sure how to handle the issue. Any help would be greatly appreciated!

--- UPDATE ---

I followed the advice below which was to downgrade my UWP framework down to 2.1.9, but when I did that I get the following error.

I have verified that I have the .NET framework installed and it is referenced correctly in my project. I am able to do a local build and run on my machine. Any ideas as to what this could be?

0 投票
1 回答
358 浏览

asp.net-core - EF Core 2.2:迁移开始报告上下文有待处理的模型更改

现有的 ASP.NET Core 2.1 应用程序(针对 netcore2.1 运行)已迁移到 ASP.NET Core 2.2(安装了 sdk 并更改了目标)。现在,每当我运行应用程序时,它都会开始显示传统的“ApplicationDbContext 有待处理的模型更改”。

如果我按照说明尝试添加迁移,我注意到它实际上会生成一个新的迁移文件。通过运行差异,我可以看到它将这些行添加到应用程序上下文快照中:

它还将以下内容添加到我的实体中:

我不确定它从哪里获得 UserServiceId1 名称(实体具有 UserServiceId 属性)。顺便说一句,这是实体类代码:

映射到该实体的表具有从 UserId、ServiceId、UserServiceId、IdentifierId 和 UserIdentifierId 构建的复合键。快照的定义如下:

哦,是的,还有用于删除 UserServiceId 列并添加“新” UserServiceId1 列的迁移文件。

我不是真正的 EF 专家,所以我不确定为什么在从 2.1 迁移到 2.2 后它停止工作。

那么,谁能指出我正确的方向?

顺便说一句,有没有办法在 ef 核心上禁用迁移?

谢谢

编辑:添加实体引用的类UserIdentifiers(仅显示类之间的关系):

最后,这是OnModelCreating方法内部执行的配置:

0 投票
2 回答
962 浏览

.net-core - 在 MySQL 上调用存储过程时,EF Core 上的转换错误(使用 Pomelo)

我在使用 EF Core 的 .Net Core 2.2 Web API 中遇到问题。我在 MySQL 数据库上调用存储过程(我使用的是 Pomelo 2.1.4)。

它抱怨无法从 DbNull 转换为 String:

错误:无法将“System.DBNull”类型的对象转换为“System.String”类型。 在 C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlDataReader.cs:第 210 行的 MySql.Data.MySqlClient.MySqlDataReader.GetString(Int32 ordinal) 在 Microsoft.EntityFrameworkCore 的 lambda_method(Closure, DbDataReader) 处。 Storage.Internal.TypedRelationalValueBufferFactory.Create(DbDataReader dataReader) 在 Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable 1.AsyncEnumerator.BufferlessMoveNext(DbContext _, Boolean buffer, CancellationToken cancellationToken) at Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func4 操作,Func 4 verifySucceeded, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable1.AsyncEnumerator.MoveNext(CancellationToken cancelToken) 在 System.Linq.AsyncEnumerable.SelectEnumerableAsyncIterator2.MoveNextCore(CancellationToken cancellationToken) in D:\a\1\s\Ix.NET\Source\System.Interactive.Async\Select.cs:line 106
at System.Linq.AsyncEnumerable.AsyncIterator
1.MoveNext(CancellationToken cancelToken) 在 D:\a\1\s\Ix.NET\Source\System.Interactive.Async\AsyncIterator.cs:Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor 1.EnumeratorExceptionInterceptor.MoveNext(CancellationToken cancellationToken) at System.Linq.AsyncEnumerable.Aggregate_[TSource,TAccumulate,TResult](IAsyncEnumerable1 源的第 98 行, T积累种子,功能3 accumulator, FuncD:\a\1\s\Ix.NET\Source\System.Interactive.Async\Aggregate.cs 中的 2 resultSelector, CancellationToken cancelToken):PropWorx.API.Controllers.FileActivitiesController.GetFileActivities(Int32 fileId, String fromDate 的第 120 行, String toDate) 在 C:\Users\fabsr\source\repos\PropWorx.API\PropWorx.API\Controllers\FileActivitiesController.cs:Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute 的第 101 行(IActionResultTypeMapper 映射器, ObjectMethodExecutor 执行器,对象控制器,Object[] 参数)在 System.Threading.Tasks.ValueTask`1.get_Result() 在 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync() 在 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker。在 Microsoft.AspNetCore.Mvc.Internal 上调用 NextActionFilterAsync()。ControllerActionInvoker.Rethrow(ActionExecutedContext context) 在 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) 在 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync() 在 Microsoft.AspNetCore .Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter() 在 Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext 上下文) 在 Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted ) 在 Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync() 的 Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()在 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync() 在 Microsoft.AspNetCore.Mvc 的 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync() 在 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) Rethrow(ActionExecutedContext context) .Internal.ResourceInvoker.InvokeNextResourceFilter() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync() 上的 Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()在 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync() 在 Microsoft.AspNetCore.Mvc 的 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync() 在 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) Rethrow(ActionExecutedContext context) .Internal.ResourceInvoker.InvokeNextResourceFilter() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync() 上的 Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()在 Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter() 在 Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext) 在 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()上下文)在 Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync() 在 Microsoft.AspNetCore.Mvc.Internal 的 Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)。 ResourceInvoker.InvokeAsync()在 Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter() 在 Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext) 在 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()上下文)在 Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync() 在 Microsoft.AspNetCore.Mvc.Internal 的 Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)。 ResourceInvoker.InvokeAsync()InvokeNextResourceFilter() 在 Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context) 在 Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) 在 Microsoft.AspNetCore.Mvc .Internal.ResourceInvoker.InvokeFilterPipelineAsync() 在 Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()InvokeNextResourceFilter() 在 Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context) 在 Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) 在 Microsoft.AspNetCore.Mvc .Internal.ResourceInvoker.InvokeFilterPipelineAsync() 在 Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
在 Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext) 在 Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext) 在 PropWorx.API.Middlewares.TenantIdentifier.Invoke(HttpContext httpContext, SharedContext sharedContext) 在 C:\Users \fabsr\source\repos\PropWorx.API\PropWorx.API\Middlewares\TenantIdentifier.cs:Microsoft.AspNetCore.Builder.Extensions.MapWhenMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Builder.Extensions.MapWhenMiddleware 的第 73 行。在 Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context) 在 Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext) 在 Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware 调用(HttpContext 上下文)。在 Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.Invoke(HttpContext context) 的 Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) 的 Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)

问题线是:

FileActivity 模型是:

最后,DbContext 中的映射是:

我整天都在绞尽脑汁想弄清楚这个……有什么想法吗?

0 投票
2 回答
977 浏览

c# - 由 C# EF 核心调用时暂停的选择查询,MSSQL 管理工作室执行查询就好了。为什么?

我不能给你足够的数据来重现错误,所以我会尽可能多地给你数据。

我有一个从 EF 核心执行的选择语句。

在 MSSQL 服务器上挂起的 SQL 查询本身变为:

在 MSSQL 管理工作室中执行时,此语句在零秒(但几毫秒)内执行。但是,C# 应用程序遇到超时。

当我在 MSSQL 上使用内部工具时,我可以看到 spid 被挂起,并且永久等待。然而,原因似乎正在改变。一开始是由于IO_COMPLETION。然后它的 SOS_YIELD_~something 最后是 PAGEIOLATCH_SH 这个最终状态它停留在

我一生都无法弄清楚为什么 MSSQL 可以毫无问题地执行查询。但是 EF 似乎没有使用索引。或者我完全错过的其他东西。

我只是没有想法。谁能指出我可能有帮助的方向?

我试过了:

在 Visual stuido 2017 中运行它。在发布模式下运行。我试过启用惰性模式,而不是使用包含。我已经尝试删除延迟加载和包含,只是为了看看我是否可以恢复预订。

没有。MSSQL 似乎拒绝 EF Core 使用索引。

问题是,只有当我提供某些参数时,查询才会挂起。其他参数工作得很好。具体来说,如果我提供不同的货币代码,那么无论查询是否暂停,这似乎都会对 MSSQL 产生影响。

我已经根据 MSSQL 管理工作室中的执行计划完全重建了高效执行此查询所需的索引。

任何可能需要的进一步信息,请告诉我,我会尽我所能,看看我能做些什么。

UPDATE 实际执行计划:

在此处输入图像描述

更新 2:我想指出,这目前正在用于开发,因此这个数据库、我的软件以及介于两者之间的任何东西都在我的“控制”之下。

就我显然缺乏经验的头脑可以控制任何事情而言:)

因此,任何关于如何更好地调试问题的建议,或对更多数据的请求都将得到积极和赞赏。并且很可能是可能的,尤其是在暗示如何提供给您的情况下!(和我)

SQL Profiler:打开与数据库的连接:

0 投票
1 回答
215 浏览

identityserver4 - IdentityServer4:调用 AddConfigurationStore 扩展方法时重用 SqlConnection

就我而言,我有几个共享同一个数据库的 EF 上下文(即,所有表都在同一个数据库上),我想在所有上下文中共享同一个 SqlConnection,以便我可以在同一个事务中应用多个更改.

为了实现这一点,我首先SqlConnection使用以下代码注册:

完成此操作后,我更改了其中一个 EF 上下文,以便它重用相同的连接:

options.UseSqlServer(provider.GetRequiredService()));

在注册配置和操作存储时,我需要做类似的事情。与 EF 扩展方法不同,没有允许我传递引用选项构建器和服务提供者的操作的重载:

builder.UseSqlServer确实有一个允许我传递连接的重载,但是如何在运行时解析 SqlConnection 以便在所有上下文中共享相同的连接?

谢谢,

路易斯

0 投票
1 回答
33 浏览

azure-sql-database - 使用多个 OrderBy 时应该创建哪个索引

我正在使用 Entity Framework Core 并读取一些包含数千条记录的数据。每条记录都有很多列,其中我使用 3 个(a、b 和 c)并执行以下操作:

OrderBy(a).ThenBy(b).ThenBy(c);

然后我正在应用排序和分页。

为了提高该查询的速度,我应该创建哪种索引(如果有)?修改表格不如阅读频繁。我应该用 3 列创建一个索引吗?按什么顺序?还是每列一个?

0 投票
1 回答
848 浏览

c# - EF Core:数据库优先转换:外键错误列

我正在尝试通过将我现有的应用程序从带有 EF6 的 .net 4.6.2 迁移到带有 EF Core 的 .net Core 2.2 来拥抱 asp.net 核心。

鉴于我有一个现有的数据库,我从数据库生成模型以开始移动。我所有的列名都使用下划线,并且外键所在的位置都以表名开头。

使用 EF Core,它坚持我的代码列名称从 Manager_UserLogin_ID 更改为 ManagerUserLoginId,这很好,我可以通过我的代码

当我尝试使用包含查询 UserLogin 和 Manager 时出现问题

尝试运行的查询尝试在 Manager 查询中包含 UserLoginId 列以及 Manager_UserLogin_ID 列。

课程如下

EF Core 上下文具有以下内容

这是来自 Sql Profiler 的查询

请问有人可以帮助我诊断并解决这个问题吗?