开发者

Linq-to-sql Compiled Query returns object NOT belonging to submitted DataContext? [duplicate]

开发者 https://www.devze.com 2022-12-23 03:47 出处:网络
This question already has answers here: Workaround for LINQ to SQL Entity Identity Caching and Compiled Query Bug?
This question already has answers here: Workaround for LINQ to SQL Entity Identity Caching and Compiled Query Bug? (2 answers) Closed 2 years ago.

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();
        // 开发者_StackOverflowGet 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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消