存储过程返回旧数据 [英] Stored Procedure returns old data

查看:145
本文介绍了存储过程返回旧数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑我正在使用EF CORE。我希望在旧的实体框架中发生这种情况。



我遇到了一个问题,我提供了不同的邮政编码转换成lat longs。

  @gLat nvarchar(50),
@gLong nvarchar(50)

DECLARE @Location geography = geography :: Point(@gLat,@ gLong,4326)

但是距离字段返回相同的距离。

  cast(@ Location.STDistance(Location)/ 1609.344 AS float )AS'Distance'
- 当回到C#



时,始终返回相同的距离如果邮政编码 XXXXXX0 给出,返回的距离为1.5
,如果使用邮政编码
YYYYYY1 调用新查询,则返回的距离仍然是1.5 .......



我想知道是否起初是因为范围,但它设置为AddScope,这意味着一个新的上下文是创建f或每个请求。

  services.AddScoped< IRepository,Repository>(); 


解决方案

我找到了解决问题的方法。 >

事实证明,在startup.cs中我的服务是AddSingleton。

  services.AddSingleton< IPersonService,PersonService>(); //需要AddScoped 
services.AddSingleton< IPersonRepository,PersonRepository>(); //还需要AddScoped

这不会工作,因为单例只会创建一次,将是每个http请求的实例。



我们需要的是每个http请求的新实例。



此外,请确保服务的范围与存储库相同。



有关更多信息,请访问此页面 services.AddTransient,service.AddScope和service.AddSingleton方法之间有什么区别? Asp.Net Core 1?



如果不正确,请对此进行任何修改!



Hope this helps =)


I'm very confused. I'm using EF CORE. I'm hoping this has happened for others in older Entity Frameworks.

I'm having a problem whereby I am providing different postcodes which are converted into lat longs.

@gLat nvarchar(50),
@gLong nvarchar(50)

DECLARE @Location geography = geography::Point(@gLat, @gLong,4326) 

however the "Distance" field is returning the same distance.

cast(@Location.STDistance(Location) / 1609.344 AS float) AS 'Distance' 
--Always returns the same distance AS THE LAST QUERY when back in C#

eg) if the postcode XXXXXX0 is given, the returned distance is 1.5 if a new query is called with postcode YYYYYY1, the distance returned is still 1.5 .......

I wondered if at first it was because of the scope, but it is set to AddScope which means a new context is create for each request.

services.AddScoped<IRepository, Repository>();

解决方案

I found a solution to my problem.

It turns out that my service, in startup.cs, was AddSingleton.

services.AddSingleton<IPersonService, PersonService>(); //Needs to be AddScoped
services.AddSingleton<IPersonRepository, PersonRepository>(); //Also needs to be AddScoped

This wouldn't have worked as singleton will only be created once and will be the same instance for every http request.

What we need is a new instance for every http request.

Also, make sure that the services' scope is the same as the repositories.

For more information you can go to this page What is the difference between services.AddTransient, service.AddScope and service.AddSingleton methods in Asp.Net Core 1?

Please make any amendments to this if it's not all correct!

Hope this helps =)

这篇关于存储过程返回旧数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆