简体   繁体   English

复合主键和错误:加载实体后数据可能已被修改或删除

[英]Composite primary key and error: data may have been modified or deleted since entities were loaded

I configured a link table with a composite key in Entity Framework Core as:我在 Entity Framework Core 中配置了一个带有复合键的链接表:

modelBuilder.Entity<TopicArticle>()
      .HasKey(ta => new { ta.TopicID, ta.ArticleID });
  modelBuilder.Entity<TopicArticle>()
      .Property(ta => ta.TopicID).ValueGeneratedNever();
  modelBuilder.Entity<TopicArticle>()
      .Property(ta => ta.ArticleID).ValueGeneratedNever();

So that I can generate this key from app, here is the part of app that generates the error:这样我就可以从应用程序生成此密钥,这是生成错误的应用程序部分:

foreach (var checkBox in model.Topics)
{
    TopicArticle ta = context.TopicArticles.AsNoTracking().FirstOrDefault(ta => ta.TopicID == checkBox.Value && ta.ArticleID == model.ArticleID);

    if (checkBox.Checked == true)
    {
        if (ta == null)
        {                        
            context.TopicArticles.Add(new TopicArticle() { TopicID = checkBox.Value, ArticleID = model.ArticleID });
        }
        else
            continue;                    
    }
    else
    {
        if (ta == null)
            continue;
        else
        {
            context.TopicArticles.Remove(new TopicArticle() { TopicID = checkBox.Value, ArticleID = model.ArticleID });
        }
    }
}

try
{
    context.SaveChanges();
}           
catch (Exception ex)
{}

The full error is:完整的错误是:

Database operation expected to affect 1 row(s) but actually affected 0 row(s).数据库操作预计会影响 1 行,但实际上影响了 0 行。 Data may have been modified or deleted since entities were loaded.自加载实体以来,数据可能已被修改或删除。 See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions有关理解和处理乐观并发异常的信息,请参阅http://go.microsoft.com/fwlink/?LinkId=527962

How to resolve this?如何解决这个问题?

You are attempting to remove a new instance of a TopicArticle instead of the referenced one that you selected earlier.您正在尝试删除TopicArticlenew实例,而不是您之前选择的引用实例。

Change your Remove to use the reference ta :更改您的Remove以使用参考ta

context.TopicArticles.Remove(ta);

暂无
暂无

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

相关问题 EF Core 数据库操作引发错误“自加载实体以来,数据可能已被修改或删除” - EF Core database operation throws error "Data may have been modified or deleted since entities were loaded" DbUpdateConcurrencyException:自加载实体以来,实体可能已被修改或删除 - DbUpdateConcurrencyException: Entities may have been modified or deleted since entities were loaded EF,更新不起作用,它表示实体可能已被修改或删除,因为实体已加载。 - EF, Update doesnt work, it says Entities may have been modified or deleted since entities were loaded. 自加载实体以来,实体可能已被修改或删除。 (ASP.NET MVC) - Entities may have been modified or deleted since entities were loaded. (ASP.NET MVC) EF Core-自从使用SQL ON UPDATE CURRENT_TIMESTAMP加载实体以来,数据可能已被修改或删除。 - EF Core - data may have been modified or deleted since entities were loaded with SQL ON UPDATE CURRENT_TIMESTAMP 自加载实体以来,实体可能已被修改或删除。 使用db.Entry(entity).State = EntityState.Modified; SaveChanges()时 - Entities may have been modified or deleted since entities were loaded. when using db.Entry(entity).State = EntityState.Modified;SaveChanges() 问题是delete语句影响了意外的行数(0)。 自加载实体以来,实体可能已被删除 - the problem is delete statement affected an unexpected number of rows (0). Entities may have been deleted since entities were loaded 存储更新,插入或删除语句会影响意外。自实体加载后修改或删除的实体 - Store update, insert, or delete statement affected an unexpected . Entities modified or deleted since entities were loaded 存储更新、插入或删除语句影响了意外数量的行 (0)。 实体可能已被修改或删除 - Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted 使用实体框架 5 更新时出错:数据可能已被修改 - Error on update with Entity Framework 5: data may have been modified
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM