Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.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
C# EF代码首先一个实体对象不能被IEntityChangeTracker的多个实例引用_C#_Entity Framework 6_Repository Pattern_Unit Of Work - Fatal编程技术网

C# EF代码首先一个实体对象不能被IEntityChangeTracker的多个实例引用

C# EF代码首先一个实体对象不能被IEntityChangeTracker的多个实例引用,c#,entity-framework-6,repository-pattern,unit-of-work,C#,Entity Framework 6,Repository Pattern,Unit Of Work,我有一个有很多答案的问题。我得到的例外是: 一个实体对象不能被多个IEntityChangeTracker实例引用 当我浏览web并在流页面上进行堆栈时,我已经达到了这样的程度:DbContext的实例在每个UnitOfWork中应该只有一个,或者必须分离上一个实例。听起来很合理!但是,我的问题是,我不知道在我的代码(张贴在下面)我必须把修改。 我一直在尝试使用通用存储库和工作单元模式加上EF代码优先的方法 public abstract class Repository<T> :

我有一个有很多答案的问题。我得到的例外是:

一个实体对象不能被多个IEntityChangeTracker实例引用

当我浏览web并在流页面上进行堆栈时,我已经达到了这样的程度:DbContext的实例在每个UnitOfWork中应该只有一个,或者必须分离上一个实例。听起来很合理!但是,我的问题是,我不知道在我的代码(张贴在下面)我必须把修改。 我一直在尝试使用通用存储库和工作单元模式加上EF代码优先的方法

public abstract class Repository<T> : IRepository<T> where T : BaseEntity
{
    protected DbContext EntityContext;
    protected readonly IDbSet<T> DbSet;

    protected Repository(DbContext context)
    {
        EntityContext = context;
        DbSet = context.Set<T>();
    }

    public virtual IEnumerable<T> GetList()
    {
        return DbSet.AsEnumerable();
    }

    public virtual T Add(T entity)
    {
        return DbSet.Add(entity);
    }
}
客户端存储库

public class ClientRepository : Repository<Client>, IClientRepository
{
    public ClientRepository(DbContext context)
        : base(context)
    {

    }

    public override Client Add(Client entity)
    {
        return DbSet.Add(entity); // <-- Here the exception comes
    }
}
public类ClientRepository:Repository,IClientRepository
{
公共ClientRepository(DbContext)
:基本(上下文)
{
}
公共覆盖客户端添加(客户端实体)
{

返回DbSet.Add(实体)//客户是否与国家有关系,您是否将其添加为
Client.country=RELATED_country
可能它试图添加同一个国家两次,我通常使用
Client.CountryId=ID_OF u country

引发错误的调用代码是什么样子我不知道这是否与问题有关,但您的IDispUnitOfWork中的osable实现与正确的模式相差甚远。例如,您没有析构函数(或者只是离开了代码示例?),因此GC.supersFinalize毫无意义……请在谷歌上搜索IDisplosable设计模式。@3dd:如果我正确理解了您的意思,则调用代码就是我添加到问题中的代码部分(最后一部分)。首先,我得到一个国家的列表,然后为每个国家生成一个新客户。@g.pickardou:我想你所说的可能也有问题。事实上,Resharper也告诉了我你提到的内容。我不知道,但可能是如果我更正了它,这将有助于我的意思是,有什么东西正在使用
ClientRespository
w那个代码看起来像什么
public class ClientRepository : Repository<Client>, IClientRepository
{
    public ClientRepository(DbContext context)
        : base(context)
    {

    }

    public override Client Add(Client entity)
    {
        return DbSet.Add(entity); // <-- Here the exception comes
    }
}
var countries = container.Resolve<ICountryService>().GetList().ToList();

        if (!countries.Any())
        {
            throw new InvalidDataGeneratorException(Strings.NoCountryToCreateClient);
        }

        var clientService = container.Resolve<IClientService>();

        clientService.Create(new Client
                             {
                                 Name = "Dell",
                                 CountryId = countries[0].Id,
                                 Country = countries[0],
                                 AddressLineOne = "76-98 Victoria Street",});
public class ClientService : Service<Client>, IClientService
{
    IUnitOfWork UnitOfWork { get; set; }
    private readonly IClientRepository _clientRepository;
    private readonly ICalculatorService _calculatorService;

    public ClientService(IClientRepository clientRepository,
        IUnitOfWork unitOfWork,
        ICalculatorService calculatorService)
        : base(clientRepository, unitOfWork)
    {
        UnitOfWork = unitOfWork;
        _calculatorService = calculatorService;
        _clientRepository = clientRepository;
    }

    public override void Create(Client client)
    {
        _clientRepository.Add(client);
        _clientRepository.Save();
    }

    }