Linq-to-sql 编译查询返回不属于提交的 DataContext 的对象?
编译查询:
public static class Machines
{
public static readonly Func<OperationalDataContext, short, Machine>
QueryMachineById =
CompiledQuery.Compile((OperationalDataContext db, short machineID) =>
db.Machines.Where(m => m.MachineID == machineID).SingleOrDefault()
);
public static Machine GetMachineById(IUnitOfWork unitOfWork, short id)
{
Machine machine;
// Old code (working)
//var machineRepository = unitOfWork.GetRepository<Machine>();
//machine = machineRepository.Find(m => m.MachineID == id).SingleOrDefault();
// New code (making problems)
machine = QueryMachineById(unitOfWork.DataContext, id);
return machine;
}
看起来编译查询正在从另一个数据上下文返回结果
[TestMethod]
public void GetMachinesTest()
{
using (var unitOfWork = IoC.Get<IUnitOfWork>())
{
// Compile Query
var machine = Machines.GetMachineById(unitOfWork, 3);
// In this unit of work everything works…
// Machine from repository (table) is equal to Machine from compile query.
}
using (var unitOfWork = IoC.Get<IUnitOfWork>())
{
var machineRepository = unitOfWork.GetRepository<Machine>();
// Get From Repository
var machineFromRepository = machineRepository.Find(m => m.MachineID == 2).SingleOrDefault();
// Get From COmpiled Query
var machine = Machines.GetMachineById(unitOfWork, 2);
VerifyMachine(machineFromRepository, 2, "Machine 2", "222222", ...);
VerifyMachine(machine, 2, "Machine 2", "222222", ...);
Assert.AreSame(machineFromRepository, machine); // FAIL
}
}
如果我运行其他(复杂)单元测试,我将得到预期的结果: 已尝试附加或添加不是新的实体,可能是从另一个 DataContext 加载的。
另一个重要信息是此测试在 TransactionScope 下(但是即使没有交易也会出现问题。)!
我正在使用通过 XML 与数据库映射的 POCO。
更新: 看起来下一个链接描述了类似的问题(这个错误解决了吗?): http://social. msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/9bcffc2d-794e-4c4a-9e3e-cdc89dad0e38
Compiled query:
public static class Machines
{
public static readonly Func<OperationalDataContext, short, Machine>
QueryMachineById =
CompiledQuery.Compile((OperationalDataContext db, short machineID) =>
db.Machines.Where(m => m.MachineID == machineID).SingleOrDefault()
);
public static Machine GetMachineById(IUnitOfWork unitOfWork, short id)
{
Machine machine;
// Old code (working)
//var machineRepository = unitOfWork.GetRepository<Machine>();
//machine = machineRepository.Find(m => m.MachineID == id).SingleOrDefault();
// New code (making problems)
machine = QueryMachineById(unitOfWork.DataContext, id);
return machine;
}
It looks like compiled query is returning result from another data context
[TestMethod]
public void GetMachinesTest()
{
using (var unitOfWork = IoC.Get<IUnitOfWork>())
{
// Compile Query
var machine = Machines.GetMachineById(unitOfWork, 3);
// In this unit of work everything works…
// Machine from repository (table) is equal to Machine from compile query.
}
using (var unitOfWork = IoC.Get<IUnitOfWork>())
{
var machineRepository = unitOfWork.GetRepository<Machine>();
// Get From Repository
var machineFromRepository = machineRepository.Find(m => m.MachineID == 2).SingleOrDefault();
// Get From COmpiled Query
var machine = Machines.GetMachineById(unitOfWork, 2);
VerifyMachine(machineFromRepository, 2, "Machine 2", "222222", ...);
VerifyMachine(machine, 2, "Machine 2", "222222", ...);
Assert.AreSame(machineFromRepository, machine); // FAIL
}
}
If I run other (complex) unit tests I'm getting as expected:
An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext.
Another Important information is that this test is under TransactionScope (But problem is appearing even without transaction around.)!
I’m using POCOs mapped with DB using XML.
UPDATE:
It looks like next link is describing similar problem (is this bug solved ?):
http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/9bcffc2d-794e-4c4a-9e3e-cdc89dad0e38
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试将上下文的 ObjectTrackingEnabled 设置为 false。这在同样的情况下对我有所帮助,但我后来在更新和插入记录时打开了它。
You can try setting the ObjectTrackingEnabled of the context to false. This helped me in this same situation but I later turn it on while updating and inserting records.