小编Muh*_*men的帖子

无法跟踪实体类型“Company”的实例,因为另一个实例具有键值?

我收到以下错误:

无法跟踪实体类型“Company”的实例,因为已跟踪具有键值“{Id: 1}”的另一个实例。附加现有实体时,请确保仅附加一个具有给定键值的实体实例。

这是我的背景:

public class Context: DbContext
{
    public Context(DbContextOptions<Context> options) : base(options)
    {
        this.ChangeTracker.LazyLoadingEnabled = false;
    }
    public DbSet<Company> Companies { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        Seed.OnModelCreating(builder);
        //builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
    }
}
Run Code Online (Sandbox Code Playgroud)

存储库库

public class EfRepository<T> : IAsyncRepository<T> where T : class
{
    protected readonly Context_context;

    public EfRepository(Context dbContext)
    {
        _context = dbContext;
    }

    public virtual T GetById(int id)
    {
        var keyValues = new object[] { id };
        return _context.Set<T>().Find(keyValues);
    }


    public List<T> List(Expression<Func<T, bool>> …
Run Code Online (Sandbox Code Playgroud)

entity-framework-core .net-core

4
推荐指数
1
解决办法
3万
查看次数

标签 统计

.net-core ×1

entity-framework-core ×1