简体   繁体   English

实体类型Menu的实例无法追踪

[英]The instance of entity type Menu cannot be tracked

I started a new asp.net core project.我开始了一个新的 asp.net 核心项目。 As you know in Core version 2.1 has dependency injection.如您所知,Core 2.1 版具有依赖注入。

I have a model called MenuModel to create a table in my database.我有一个名为 MenuModel 的 model 来在我的数据库中创建一个表。 I added an interface called IMenuRepository for my model and a class call MenuRepository that implements my Interface.我为我的 model 添加了一个名为 IMenuRepository 的接口,并为实现我的接口的 class 调用 MenuRepository 添加了一个接口。

I try to edit my menu in my repository like this我尝试像这样在我的存储库中编辑我的菜单

    public async Task Edit(Menu menu)
    {
        menu.UpdatedBy = _userId;
        menu.UpdatedDate = DateTime.Now;

        _db.Entry(menu).State = EntityState.Modified;
        await _db.SaveChangesAsync();
    }

I added this line of code to my startup.cs for dependency injection我将这行代码添加到我的 startup.cs 中以进行依赖注入

services.AddTransient<IMenuRepository, MenuRepository>();

I found another question that asked the same question and the answer says I have to change my startup.cs code like:我发现另一个问同样问题的问题,答案说我必须更改我的 startup.cs 代码,例如:

services.AddScoped<IMenuRepository, MenuRepository>();

I did and I tried the others as well but I get this error always.我做了,我也尝试了其他的,但我总是得到这个错误。 I searched a couple of times but I can't find any solution to my problem.我搜索了几次,但找不到任何解决问题的方法。

One time I detached my model from my DatabaseContext before editing and I got the same error as well.有一次我在编辑之前将 model 从我的 DatabaseContext 中分离出来,我也遇到了同样的错误。

I tried with this code to update but no result我尝试使用此代码进行更新,但没有结果

_db.Update(menu);

Here is my error description:这是我的错误描述:

**An unhandled exception occurred while processing the request. **处理请求时发生未处理的异常。

InvalidOperationException: The instance of entity type 'Menu' cannot be tracked because another instance with the same key value for {'MenuId'} is already being tracked. InvalidOperationException:无法跟踪实体类型“Menu”的实例,因为已经在跟踪具有相同键值 {'MenuId'} 的另一个实例。 When attaching existing entities, ensure that only one entity instance with a given key value is attached.附加现有实体时,请确保仅附加一个具有给定键值的实体实例。 Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.考虑使用“DbContextOptionsBuilder.EnableSensitiveDataLogging”来查看冲突的键值。 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap.ThrowIdentityConflict(InternalEntityEntry entry)** Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap.ThrowIdentityConflict(InternalEntityEntry 条目)**

On the other hand, I have a part of the code for publishing and unpublish menu that changes the record status and that edit the menu like this and it works perfectly.另一方面,我有一部分用于发布和取消发布菜单的代码,它可以更改记录状态并像这样编辑菜单,并且效果很好。

    public async Task Publish(int id)
    {
        Menu menu = await Find(id);

        menu.UpdatedBy = _userId;
        menu.UpdatedDate = DateTime.Now;
        menu.Status = eStatus.Published;

        _db.Entry(menu).State = EntityState.Modified;
    }

The difference is only I send the menu id to publish method and send menu obj to edit.不同之处仅在于我将菜单 id 发送到发布方法,并将菜单 obj 发送到编辑。

After searching in different languages I found the answer to my question在用不同的语言搜索后,我找到了我的问题的答案

I changed my edit method to this:我将编辑方法更改为:

    public async Task Edit(Menu menu)
    {
        menu.UpdatedBy = _userId;
        menu.UpdatedDate = DateTime.Now;

        var editVersion = await Find(menu.MenuId);
        if (editVersion != null)
        {
            _db.Entry(editVersion).CurrentValues.SetValues(menu);
        }
        await _db.SaveChangesAsync();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法跟踪实体类型的实例 - The instance of entity type cannot be tracked 实体类型“”的实例无法跟踪 - The instance of entity type '' cannot be tracked 实体类型的实体框架实例无法跟踪 - Entity Framework instance of entity type cannot be tracked HostedService:无法跟踪实体类型的实例 - HostedService: The instance of entity type cannot be tracked 即使使用 AsNoTracking 也无法跟踪实体类型的实例 - The instance of entity type cannot be tracked even with AsNoTracking InvalidOperationException:无法跟踪实体类型&#39;ApplicationUser&#39;的实例 - InvalidOperationException: The instance of entity type 'ApplicationUser' cannot be tracked 无法跟踪实体类型“BookLoan”的实例 - The instance of entity type 'BookLoan' cannot be tracked InvalidOperationException:无法跟踪实体类型“Vessels”&gt; 的实例,因为另一个实例 - InvalidOperationException: The instance of entity type 'Vessels' > cannot be tracked because another instance 无法跟踪实体类型“Entity”的实例,因为已跟踪另一个具有与 {'Id'} 相同键值的实例 - The instance of entity type 'Entity' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked 实体框架核心 - 无法跟踪实体类型的实例,因为已在跟踪具有键值的另一个实例 - Entity framework Core - The instance of entity type cannot be tracked because another instance with the key value is already being tracked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM