Linq-to-sql 编译查询返回不属于提交的 DataContext 的对象?

发布于 2024-08-25 21:31:38 字数 2320 浏览 5 评论 0原文

编译查询:

   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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执手闯天涯 2024-09-01 21:31:38

您可以尝试将上下文的 ObjectTrackingEnabled 设置为 false。这在同样的情况下对我有所帮助,但我后来在更新和插入记录时打开了它。

DBDataContext.ObjectTrackingEnabled = false; // Read Only

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.

DBDataContext.ObjectTrackingEnabled = false; // Read Only
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文