Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.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# 实体类型的实例'';无法跟踪_C#_Entity Framework_Asp.net Core_Single Page Application_Progressive Web Apps - Fatal编程技术网

C# 实体类型的实例'';无法跟踪

C# 实体类型的实例'';无法跟踪,c#,entity-framework,asp.net-core,single-page-application,progressive-web-apps,C#,Entity Framework,Asp.net Core,Single Page Application,Progressive Web Apps,我正在发送授权请求,在授权的方法控制器中,我正在尝试为通过授权的用户更新实体,但我有一个错误: 无法跟踪实体类型“SUsers”的实例,因为已跟踪键值为“{Id:1}”的另一个实例。附着现有实体时,请确保仅附着一个具有给定键值的实体实例 使用的堆栈 asp core 2.2、spa、vue、pwa、jwt、automapper 8.8.4、Microsoft.EntityFrameworkCore 2.2.4 版本 净核心2.2 Microsoft.EntityFrameworkCore 2.

我正在发送授权请求,在授权的方法控制器中,我正在尝试为通过授权的用户更新实体,但我有一个错误:

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

使用的堆栈 asp core 2.2、spa、vue、pwa、jwt、automapper 8.8.4、Microsoft.EntityFrameworkCore 2.2.4

版本
  • 净核心2.2
  • Microsoft.EntityFrameworkCore 2.2.4
  • Microsoft.EntityFrameworkCore.InMemory 2.2.4
  • Microsoft.EntityFrameworkCore.Design 2.2.4
  • Microsoft.EntityFrameworkCore.SqlServer 2.2.4
0,DI
公共静态类StartupExtension
{
公共静态IServiceCollection AddDependencies(此IServiceCollection _IServiceCollection,IConfiguration AppConfiguration)
{
#区域数据
字符串ID=System.Guid.NewGuid().ToString();
_iServiceCollection.AddDbContext(opt=>
{
选择使用内存数据库(ids);
});
_iServiceCollection.AddScoped(provider=>provider.GetService());
#端区
#区域自动映射器
var config=new-MapperConfiguration(cfg=>{
AddMaps(“PWSPA.WEB”、“PWSPA.BLL”);
});
config.assertconfigurationsvalid();
#端区
#区域存储库
_AddScoped(typeof(IGenericRepository)、typeof(genericpository));
_iServiceCollection.AddScoped();
#端区
#地区服务
#区域映射服务
_AddScoped(typeof(IGenericMapperService),typeof(GenericMapperService));
_AddScoped(typeof(IMapperService),typeof(MapperService));
#端区
_iServiceCollection.AddScoped();
#端区
返回_iServiceCollection;
}
}
1.Api控制器
公共类AuthController:BaseApiController
{
私有只读ILogger_日志;
私有只读安全设置_config;
私有只读iUserify\u SigningMgr;
私有只读IAuthService _IAuthService;
[异名]
[HttpPost(“登录”)]
公共IActionResult登录([FromBody]RequestTokenApiModel模型)
{
尝试
{
SUsersDTO user=null;
user=\u iAuthService.SingleOrDefault(u=>
u、 WindowsLogin.ToLower()=“guest”);
user.WindowsLogin=“guest”;
/*
无法跟踪实体类型“SUsers”的实例
因为另一个
键值为“{Id:1}”的实例已被删除
跟踪,什么时候
附加现有实体时,请确保只有一个实体
举例说明
随附给定的键值。
*/
countUpdate=\u iAuthService.Update(用户);
}
捕获(参数异常)
{
返回请求(例如消息);
}
捕获(例外情况除外)
{
_log.LogError(例如,例如消息);
返回状态码(500,例如消息);
}
}
}
2.服务
公共类AuthService:ServiceBase,IAuthService
{
公共AuthService(IUnitOfWork uow,IMapperService MapperService):基本(uow,MapperService)
{
Repository.Query().Include(u=>u.Role.Load();
}
...
}
公共类ServiceBase:IGenericService where TModel:class where tModelTo:class
{
专用只读IUnitOfWork数据库;
专用只读IMapperService(映射服务);
私有只读IGenericRepository genericRepository;
专用只读IGenericMapperService genericMapService;
公共服务库(IUnitOfWork uow、IMapperService、IMapperService)
{
_MapService=iMapperService;
db=uow;
genericRepository=uow.Repository();
genericMapService=_MapService.Map();
}
受保护的虚拟类型ObjectType=>typeof(TModel);
受保护的虚拟IGenericRepository存储库=>genericRepository;
受保护的虚拟IMapperService MapService=>\u MapService;
受保护的虚拟IGenericMapperService映射=>genericMapService;
受保护的虚拟IUnitOfWork数据库=>db;
...
公共int更新(TModelDTO entityDto)
{
var entity=Map.For(entityDto);
返回Repository.Update(实体);
}
}
3.回购协议
公共类通用存储库:
IGenericRepository,其中tenty:类
{
私有只读IBaseDbContext\u上下文;
私人只读i工作单元(unitof工作单元);;
私有只读字符串errorMessage=string.Empty;
公共通用存储库(IBaseDbContext context,IMapper _IMapper)/:base(context,_IMapper)
{
_上下文=上下文;
_unitOfWork=新的unitOfWork(上下文,_iMapper);
}
公共类型ObjectType=>typeof(tenty);
受保护的虚拟IBaseDbContext DbContext=>\u context;
受保护的虚拟DbSet DbSet=>_context.Set();
...
公共int更新(TEntity更新)
{
如果(已更新==null)
{
返回0;
}
附件(更新版);
_context.Entry(updated).State=EntityState.Modified;
返回Save();
}
...
私有int Save()
{
尝试
{
返回工作单元