Linq to SQL-更新后的脏读-WCF服务 [英] Linq to SQL - Dirty Reads after Updating - WCF Service

查看:141
本文介绍了Linq to SQL-更新后的脏读-WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个dbml文件(从linq到sql)可以连接到我的数据库.我有一个使用通用存储库(用于每个实体)与dbml对话的管理器.经理使用仓库进行工作(CRUD操作). WCF包装了管理器,并将其公开给外界(以便我的应用程序可以使用它).

I have a dbml file (linq to sql) to connect to my database. I have a manager that uses a generic repository (for each entity) to talk to the dbml. A manager uses the Repo to do work (CRUD operations). A WCF wraps the manager and exposes the it to the outside world (so my application can use it).

DB<-> IRepository<实体,DataContext><-> IManager(IRepo,IRepo ...)<-> WCFService(IManager)

DB <--> IRepository< Entity, DataContext> <--> IManager(IRepo, IRepo...) <--> WCFService(IManager)

我已经测试过经理,每次都能正常工作.问题是WCF.我将读取数据(例如GetAllLocations),更新数据(例如更改位置名称),但是稍后在某个随机点上,我将读取脏数据.例如.如果我一直在将位置名称从"BC 1"更改为"BC 2",再更改为"BC 3",则有时我会读取它并获得较旧的值.就像在将其更改为"BC 3"并希望阅读"BC 3"之后,我得到了"BC 1"(由于更新之前的值为"BC 2",所以没有意义). WCF默认情况下没有缓存,那么为什么在我的WCF而不是我的管理器上发生这种情况? WCF所做的只是将值传递给管理器并从管理器获取值,这是一个非常基本的包装器类.

I've tested the manager, it works fine every time. The problem is the WCF. I'll read data (e.g. GetAllLocations), update the data (e.g. change a Location name), but then at some random point later, I'll read dirty data. E.g. if I've been changing a location name to from "BC 1", to "BC 2", to "BC 3", sometimes I'll read it and get older values. Like after I've changed it to "BC 3" and I am expecting to read "BC 3", I get "BC 1" (which doesn't make sense since the value before the update was "BC 2" anyway). WCF does not have caching by default, so why is this happening on my WCF and NOT on my manager? All the WCF does is pass values to the manager and get values from the manager, it's a very basic wrapper class.

注意:我正在使用StructureMap自动解决DependencyInjection和IoC问题.

NOTE: I am using StructureMap to automatically resolve DependencyInjection and IoC stuff.

问题方法:正在读取的所有内容(例如,GetLocationById和GetAllLocations).他们只是不总是从数据库返回最新数据.我知道它不是管理器,因为我创建了一个简单的项目来分别测试管理器和WCF,并且只有WCF在更新数据后会脏读(特别是位置").我尚未测试所有其他实体.

Problem methods: anything that's reading (e.g. GetLocationById and GetAllLocations). They just don't always return the latest data from the database. I know it's not the manager because I created a simple project to test both the Manager and WCF independently, and only the WCF had dirty reads after updating data (specifically the Location). I have not testing all the other entities yet.

最后一个注意事项:我不断收到"System.Data.Linq.ChangeConflictException:未找到或更改行."例外.我将DBML设计器的位置实体"的属性(PK除外)更改为更新检查:从不".无论如何,在进行此更改之前发生了脏读,并且管理器工作正常(并且使用DBML).因此,我没有理由相信这会导致脏读.

One final note: I kept getting the "System.Data.Linq.ChangeConflictException: Row not found or changed." exception. I changed the DBML designer Location Entity's properties (except the PK) to Update Check: Never. The dirty reads were happening before this change anyway and the manager works fine (and it uses the DBML). So I have no reason to believe this is causing the dirty reads.

此外,位置实体在数据库中有一个触发器,但是由于我禁用了它,并且没有帮助,所以我消除了这个原因,并且经理再次正常工作.

Also, the Location Entity has a trigger in the database, but I'm eliminated this as the cause because I disabled it and that didn't help, and once again, the manager works fine.

推荐答案

原来这是由于使用WCF和结构图而导致的缓存问题.在数据注册表中注册数据上下文时,我更改了默认结构映射缓存.

Turns out this was a caching issue caused by using WCF and Structure Map. I'd changed the default structure map caching when registering my data contexts in the data registry.

For<MyDataContext>()
   //.HybridHttpOrThreadLocalScoped()
   .Use(() => new MyDataContext());

默认为每次通话,这是我所需要的.

The default was per call which is what I needed.

这篇关于Linq to SQL-更新后的脏读-WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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