【问题标题】:Stored Procedure returns old data存储过程返回旧数据
【发布时间】:2017-01-17 19:12:26
【问题描述】:

我很困惑。我正在使用 EF CORE。我希望这发生在旧实体框架中的其他人身上。

我遇到了一个问题,我提供了不同的邮政编码,这些邮政编码被转换为经纬度。

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

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

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

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

eg) 如果给定邮政编码 XXXXXX0,则返回距离为 1.5 如果使用邮政编码调用新查询 YYYYYY1,返回的距离还是1.5.......

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

services.AddScoped<IRepository, Repository>();

【问题讨论】:

    标签: asp.net-mvc entity-framework stored-procedures geolocation asp.net-core


    【解决方案1】:

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

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

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

    这是行不通的,因为单例只会被创建一次,并且对于每个 http 请求都是同一个实例。

    我们需要为每个 http 请求创建一个新实例。

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

    更多信息可以到这个页面What is the difference between services.AddTransient, service.AddScope and service.AddSingleton methods in Asp.Net Core 1?

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

    希望这会有所帮助 =)

    【讨论】:

      猜你喜欢
      • 2018-08-20
      • 2011-10-25
      • 2017-12-29
      • 2021-11-23
      • 1970-01-01
      • 1970-01-01
      • 2018-11-19
      • 2016-08-19
      • 2014-06-27
      相关资源
      最近更新 更多