POST http://localhost:52054/api/Authentication/DeleteAccesstoken HTTP/1.1 Host: localhost:52054 Content-Type: application/json {"id":1,"userName":"mnoureldin","accesstoken":{"id":1,"token":"123ABC456EFG","userId":1}}
我的控制器(在EF核心中)处理如下:
[HttpPost] public IActionResult DeleteAccesstoken([FromBody]User user) { using (var Context = new UnitOfWork().Context) { var userEntity = Context.Users.Find(user.Id); // Get the real entity of the user received as json if (userEntity != null) { var accesstokenEntity = Context.Accesstokens.Find(userEntity.Accesstoken.Id); // Find the entity of the accesstoken (I tried also directly by accessing the navigation property of user entity) Context.Accesstokens.Remove(accesstokenEntity); return Ok(); } else { return Unauthorized(); } } }
但行Context.Accesstokens.Remove(accesstokenEntity);抛出此异常:
An exception of type ‘system.invalidOperationException’ occurred in
Microsoft.EntityFrameworkCore.dll but was not handled in user codeAdditional @R_112_4045@ion: The instance of entity type ‘Accesstoken’
cannot be tracked because another instance of this type with the same
key is already being tracked. When adding new entities,for most key
types a unique temporary key value will be created if no key is set
(i.e. if the key property is assigned the default value for its type).
If you are explicitly setting key values for new entities,ensure they
do not collide with existing entities or temporary values generated
for other new entities. When attaching existing entities,ensure that
only one entity instance with a given key value is attached to the
context.
我还尝试直接从userEntity访问Accesstoken导航属性,但具有相同的异常.
这是我的UnitOfWork初始化:
public UnitOfWork() { // Configure EF connection var optionsBuilder = new DbContextOptionsBuilder<CustomDbContext>(); optionsBuilder .UseMysqL(@"server=192.168.1.35; port=3306; sslmode=none; userid=root; pwd=P@ssword; database=dotnet;"); Context = new CustomDbContext(optionsBuilder.Options); // Configure data loading method to explicit Context.Accesstokens.Load(); }
我的CustomBdContext:
public class CustomDbContext : DbContext { // Tell EF to map the entities to tables public DbSet<User> Users { get; set; } public DbSet<Accesstoken> Accesstokens { get; set; } public CustomDbContext(DbContextOptions options) : base(options) { } }
我有以下简单的数据模型与一对一的关系:
User ----- Accesstoken
用户:
public class User { public int Id { get; set; } public string UserName { get; set; } public virtual Accesstoken Accesstoken { get; set; } }
的accesstoken:
public class Accesstoken { public int Id { get; set; } public string Token { get; set; } [ForeignKey("User"),required] public int UserId { get; set; } public virtual User User { get; set; } }
有人可以帮我解决这个问题吗?我不明白到底发生了什么……
解决方法
尝试
[HttpPost] public IActionResult DeleteAccesstoken([FromBody]User user) { // Requires System.Linq if (Context.Users.Any(u => u.Id == user.Id)) { var accesstokenEntity = Context.Accesstokens.Find(user.Accesstoken.Id); // Find the entity of the accesstoken (I tried also directly by accessing the navigation property of user entity) Context.Accesstokens.Remove(accesstokenEntity); // NOTE: You re not saving? return Ok(); } else { return Unauthorized(); } }
或者尝试:
[HttpPost] public IActionResult DeleteAccesstoken([FromBody]User user) { if (Context.Users.Any(u => u.Id == user.Id)) { Context.Accesstokens.Remove(user.Accesstoken); return Ok(); } else { return Unauthorized(); } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。