我正在发送授权请求,在用于授权的方法控制器中,我正在尝试为已通过授权的用户更新实体,但是出现错误:
无法跟踪实体类型'SUsers'的实例,因为已经跟踪了另一个键值为'{Id:1}'的实例。附加现有实体时,请确保仅附加一个具有给定键值的实体实例。
asp core 2.2,spa,vue,pwa,jwt,automapper 8.8.4,Microsoft.EntityFrameworkCore 2.2.4
public static class StartupExtension
{
public static IServiceCollection AddDependencies(this IServiceCollection _iServiceCollection, IConfiguration AppConfiguration )
{
#region Data
string ids = System.Guid.NewGuid().ToString();
_iServiceCollection.AddDbContext<BaseDbContext, FakeDbContext>(opt =>
{
opt.UseInMemoryDatabase(ids);
});
_iServiceCollection.AddScoped<IBaseDbContext>(provider => provider.GetService<BaseDbContext>());
#endregion
#region AutoMapper
var config = new MapperConfiguration(cfg => {
cfg.AddMaps("PWSPA.WEB", "PWSPA.BLL");
});
config.AssertConfigurationIsValid();
#endregion
#region Repository
_iServiceCollection.AddScoped(typeof(IGenericRepository<>), typeof(GenericRepository<>));
_iServiceCollection.AddScoped<IUnitOfWork, UnitOfWork>();
#endregion
#region service
#region mapper service
_iServiceCollection.AddScoped(typeof(IGenericMapperService<,>), typeof(GenericMapperService<,>));
_iServiceCollection.AddScoped(typeof(IMapperService), typeof(MapperService));
#endregion
_iServiceCollection.AddScoped<IAuthService, AuthService>();
#endregion
return _iServiceCollection;
}
}
public class AuthController : BaseApiController
{
private readonly ILogger _log;
private readonly SecuritySettings _config;
private readonly IUserVerify _signInMgr;
private readonly IAuthService _iAuthService;
[AllowAnonymous]
[HttpPost("login")]
public IActionResult Login([FromBody] RequestTokenApiModel model)
{
try
{
SUsersDTO user = null;
user = _iAuthService.SingleOrDefault(u =>
u.WindowsLogin.ToLower() == "guest");
user.WindowsLogin = "guest";
/*
The instance of entity type 'SUsers' cannot be tracked
because another
instance with the key value '{Id: 1}' is already being
tracked. When
attaching existing entities, ensure that only one entity
instance with a
given key value is attached.
*/
countUpdate = _iAuthService.Update(user);
}
catch (ArgumentException ex)
{
return BadRequest(ex.Message);
}
catch (Exception ex)
{
_log.LogError(ex, ex.Message);
return StatusCode(500, ex.Message);
}
}
}
public class AuthService : ServiceBase<SUsers, SUsersDTO>, IAuthService
{
public AuthService(IUnitOfWork uow, IMapperService MapperService) : base(uow, MapperService)
{
Repository.Query().Include(u => u.Role).Load();
}
...
}
public class ServiceBase<TModel, TModelDTO> : IGenericService<TModelDTO> where TModel : class where TModelDTO : class
{
private readonly IUnitOfWork db;
private readonly IMapperService _MapService;
private readonly IGenericRepository<TModel> genericRepository;
private readonly IGenericMapperService<TModel, TModelDTO> genericMapService;
public ServiceBase(IUnitOfWork uow, IMapperService iMapperService)
{
_MapService = iMapperService;
db = uow;
genericRepository = uow.Repository<TModel>();
genericMapService = _MapService.Map<TModel, TModelDTO>();
}
protected virtual Type ObjectType => typeof(TModel);
protected virtual IGenericRepository<TModel> Repository => genericRepository;
protected virtual IMapperService MapService => _MapService;
protected virtual IGenericMapperService<TModel, TModelDTO> Map => genericMapService;
protected virtual IUnitOfWork Database => db;
...
public int Update(TModelDTO entityDto)
{
var entity = Map.For(entityDto);
return Repository.Update(entity);
}
}
public class GenericRepository<TEntity> :
IGenericRepository<TEntity> where TEntity : class
{
private readonly IBaseDbContext _context;
private readonly IUnitOfWork _unitOfWork;
private readonly string errorMessage = string.Empty;
public GenericRepository(IBaseDbContext context, IMapper _iMapper) //: base(context, _iMapper)
{
_context = context;
_unitOfWork = new UnitOfWork(context, _iMapper);
}
public Type ObjectType => typeof(TEntity);
protected virtual IBaseDbContext DbContext => _context;
protected virtual DbSet<TEntity> DbSet => _context.Set<TEntity>();
...
public int Update(TEntity updated)
{
if (updated == null)
{
return 0;
}
DbSet.Attach(updated);
_context.Entry(updated).State = EntityState.Modified;
return Save();
}
...
private int Save()
{
try
{
return _unitOfWork.Commit();
}
catch (DbUpdateException e)
{
throw new DbUpdateException(e.Message, e);
}
}
public class UnitOfWork : IUnitOfWork
{
private readonly IBaseDbContext _dbContext;
private readonly Dictionary<Type, object> _repositories = new Dictionary<Type, object>();
private readonly IMapper _iMapper;
public Dictionary<Type, object> Repositories
{
get => _repositories;
set => Repositories = value;
}
public UnitOfWork(IBaseDbContext dbContext, IMapper _iMapper)
{
_dbContext = dbContext;
this._iMapper = _iMapper;
}
public IGenericRepository<TEntity> Repository<TEntity>() where TEntity : class
{
if (Repositories.Keys.Contains(typeof(TEntity)))
{
return Repositories[typeof(TEntity)] as IGenericRepository<TEntity>;
}
IGenericRepository<TEntity> repo = new GenericRepository<TEntity>(_dbContext, _iMapper);
Repositories.Add(typeof(TEntity), repo);
return repo;
}
public EntityEntry<TEintity> Entry<TEintity>(TEintity entity) where TEintity : class
{
return _dbContext.Entry(entity);
}
...
}
public int Update(TEntity updated)
{
if (updated == null)
{
return 0;
}
/*
on line DbSet.Attach(updated) an exception occurs
*/
DbSet.Attach(updated);
_context.Entry(updated).State = EntityState.Modified;
return Save();
}
public int Update(TModelDTO entityDto)
{
var entity = Map.For(entityDto);
return Repository.Update(entity);
}
实体更新
无法跟踪实体类型'SUsers'的实例,因为已经跟踪了另一个键值为'{Id:1}'的实例。附加现有实体时,请确保仅附加一个具有给定键值的实体实例。
1.ThrowIdentityConflict(InternalEntityEntry entry)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap
中的 1.Add(TKey键,InternalEntityEntry条目,布尔值updateDuplicate)
在Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.StartTracking(InternalEntityEntry条目)
在Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState oldState,EntityState newState,Boolean acceptChanges)
在Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.PaintAction(EntityEntryGraphNode节点,布尔值)
在Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph [TState](EntityEntryGraphNode节点,TState状态,Func 3 handleNode)
at Microsoft.EntityFrameworkCore.DbContext.SetEntityState[TEntity](TEntity entity, EntityState entityState)
at PWSPA.DAL.Repositories.GenericRepository
1.Update(已更新TEntity)在D:\ repos \ asp-core-2.2-clean2 \ PWSPA中.DAL \ Repositories \ GenericRepository.cs:第99行
在D:\ repos \ asp-core-2.2-clean2 \ PWSPA.BLL \ Services \ ServiceBase.cs:line 208中的PWSPA.BLL.Services.ServiceBase`2.Update(TModelDTO entityDto)
在D:\ repos \ asp-core-2.2-clean2 \ PWSPA.WEB \ API \ AuthController.cs:第90行的PWSPA.API.Controllers.AuthController.Login(RequestTokenApiModel模型)中
答案 0 :(得分:2)
您在这里犯了一个新手错误,即实际上只是转储您可以想到的所有信息,但是具有讽刺意味的是,您错过了唯一真正重要的部分:_iAuthService
后面的代码。仅发布与问题直接相关的代码。如果我们需要其他东西,我们总是可以要求它。并且,就此而言,发布与该问题直接相关的 all 代码。如果错误是由于您编写的自定义服务类引起的,请发布该服务类。
也就是说,您遇到的错误归结为以下情况。有时,您查询一个实体,将其添加到上下文的对象跟踪中。然后,您稍后尝试更新该实体的非跟踪版本,而不是查询的版本。这可能是由于从模型绑定器接收到它(即,它是动作的一个参数),或者实际上是使用new
实例化了一个实例,或者只是使用了上下文的另一个实例来检索它(并将其保存到另一个实例中) )。
根据您提供的代码,我的钱就放在最后一个。您可能没有在服务类中正确处理上下文,并且正在使实体从上下文的一个实例进行修改,并尝试使用上下文的另一个实例进行更新。应该始终插入上下文,以确保在整个生命周期(请求)中始终使用相同的实例。换句话说,如果您正在执行using (var context = new MyContext())
或实际上是任何new MyContext()
,那就是您的问题。
答案 1 :(得分:0)
由于我使用了自动映射器,因此我的问题已使用AutoMapper.Collection
解决了我的解决问题
//using AutoMapper;
//using AutoMapper.Configuration;
//using AutoMapper.EquivalencyExpression;
//using AutoMapper.Extensions.ExpressionMapping;
services.AddAutoMapper (assemblyes);
MapperConfigurationExpression configExpression = new MapperConfigurationExpression ();
configExpression.AddCollectionMappers ();
configExpression.AddExpressionMapping ();
configExpression.UseEntityFrameworkCoreModel <BaseDbContext> (services.BuildServiceProvider ().
CreateScope (). ServiceProvider);
configExpression.AddMaps (assemblyes);
Mapper.Initialize (configExpression);
Mapper.Configuration.AssertConfigurationIsValid ();
//using AutoMapper.EntityFrameworkCore;
//using Microsoft.EntityFrameworkCore;
public static class RepoExtensions
{
public static TModel InsertOrUpdate<TModel, TModelDto>(this IRepository repository, TModelDto modelDto) where TModel : BaseEntity where TModelDto :
BaseEntityDTO
{
TModel model = repository.DbSet<TModel>().Persist().InsertOrUpdate(modelDto);
repository.Save();
return model;
}
public static async Task<TModel> InsertOrUpdateAsync<TModel, TModelDto>(this IRepository repository, TModelDto modelDto) where TModel : BaseEntity where TModelDto :
BaseEntityDTO
{
TModel model = repository.DbSet<TModel>().Persist().InsertOrUpdate(modelDto);
await repository.SaveAsync();
return model;
}
}
之前
public int Update(TModelDTO entityDto) { var entity = Map.For(entityDto); return Repository.Update(entity); }
之后
public TModel Update(TModelDTO entityDto)
{
return Repository.InsertOrUpdate<TModel, TModelDTO>(entityDto);
}
p.s该示例所引用的存储库未更新