Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 使用EF更新通用存储库的问题_Entity Framework_Repository Pattern - Fatal编程技术网

Entity framework 使用EF更新通用存储库的问题

Entity framework 使用EF更新通用存储库的问题,entity-framework,repository-pattern,Entity Framework,Repository Pattern,我已经创建了通用存储库,我需要在事务中更新两个实体。 这就是我正在做的 ProductOrganizationEntity poDataContext= new ProductOrganizationEntity(); IRepository<tblProductInfo> productRepo = new GenericRepository<ProductOrganizationEntity, tblConfirmation>(poDataContext); pro

我已经创建了通用存储库,我需要在事务中更新两个实体。 这就是我正在做的

ProductOrganizationEntity poDataContext= new ProductOrganizationEntity();
IRepository<tblProductInfo> productRepo = new GenericRepository<ProductOrganizationEntity, tblConfirmation>(poDataContext);
productRepo.SaveChanges()这就是它给我带来错误的地方。错误是

The operation could not be performed because OLE DB provider "SQLNCLI10" for linked server "Venus" was unable to begin a distributed transaction.
(我们确实有一个名为Venus的服务器,但在这些事务中它无论如何都不能访问。第二,正如我所说的,它在没有事务块的情况下工作)

如果从事务块中取出,这段代码可以正常工作


ProductLocation.SaveLocation正在为位置创建存储库。这是保存位置的代码

IRepository<LocationInfo> locRepo= new GenericRepository<ProductOrganizationEntity, LocationInfo>(new ProductOrganizationEntity());

 if (loc.locID <= 0) // This is a new Location to be added.
      locRepo.Add(locEntity);
 else
     locRepo.Attach(siteToAdd);

 locRepo.SaveChanges();
这就是我在我的普通回购协议中保存更改的内容

 public virtual void SaveChanges()
    {
        if (_dataContext == null)
            throw new Exception("SaveChanges: DataContext is not initialized.");

        _dataContext.SaveChanges();
    }
我在这里做错了什么


非常感谢您的指点。

您的服务器可能已链接到数据库级别的另一台SQL server

也许看看这个:


必须承认我从未使用过链接服务器(至少还没有),但看到错误中的“链接服务器”让我想到了这一点。

您需要在单个事务范围内执行此操作吗?能否创建ProductLocation并附加productEntity,然后保存更改?这将防止您的系统需要DTC,而DTC是您错误的根源。我真的不明白为什么会在这里发布DTC。此代码在没有TransactionScope的情况下运行良好。插入件在TransactionScope中工作良好。但是,我没有完全理解您的意思,您可以创建ProductLocation并附加productEntity,然后保存更改吗?THX您的ProductLocation().SaveLocation()代码实际上在做什么?请参阅调用DTC的原因。ProductLocation()。甚至没有调用SaveLocation()。当我调用productRepo.SaveChanges()时,它因错误而崩溃;我验证了从TransactionBlock中删除所有与位置相关的内容后,仍然会抛出错误。它是否与我存储库中用于附加的代码有关?可能-u dataContext.Entry(实体)可能正在进行DB调用,但老实说,我不太确定。无论哪种方式,DTC肯定会被你的下一行代码调用,因为它正在调用一个新的回购协议,并试图打开一个新的连接。如果您不能在服务器上启用DTC(听起来好像不能),那么您将无法在单个作用域中执行此操作。为什么您需要在单个范围内完成这些工作?你担心比赛条件吗?你可以做一些类似于工作单元模式的事情,所有回购协议共享一个上下文,然后你可以调用一个单独的保存更改。我不确定这是因为。插入在事务范围内工作正常。当TransactionScope被取出时,此更新代码可以正常工作。唯一的问题是一旦将其放入TransactionScope中。我知道那个错误了。嗨@TabishSarwar,你最后是怎么解决的?Alex,有一个其他开发人员没有记录的触发器,从来没有被使用过。该触发器试图从另一个链接服务器中选择某个内容并进行更新。我所要做的就是扣下扳机,它就成功了。从技术上讲,我所做的一切都没有问题。很酷,很高兴你弄清了真相。这肯定是一个棘手的问题。
   public void Attach(TEntity entity)
    {
        if (entity == null)
            throw new ArgumentException("Update : Supplied Entity is Null.");

        _currentDbSet.Add(entity);
        System.Data.Entity.Infrastructure.DbEntityEntry entry = _dataContext.Entry(entity);
        entry.State = System.Data.EntityState.Modified;
    }
 public virtual void SaveChanges()
    {
        if (_dataContext == null)
            throw new Exception("SaveChanges: DataContext is not initialized.");

        _dataContext.SaveChanges();
    }