【问题标题】:Inserting Item in Database: Store update, insert, or delete statement affected an unexpected number of rows (0)在数据库中插入项目:存储更新、插入或删除语句影响了意外的行数 (0)
【发布时间】:2021-01-14 13:13:08
【问题描述】:

我尝试编写一个简单的创建方法,就像我构建了几次一样,但这一次,我一遍又一遍地收到此错误:

Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions.

我还尝试了异常给出的链接中的推荐程序:

            catch (DbUpdateConcurrencyException dbex)
            {
                dbex.Entries.Single().Reload();
            }

但随后出现以下错误:

Refresh cannot be used for entities in the Added state

什么可以理解,我调用了 Add-Method 应该没什么可刷新的吧?

我在网上找到的大多数案例都是针对更新方法的,所以不是我要找的。​​p>

我也试过这个,但是异常不是由错误触发的。 捕捉(DbUpdatableDataRecord ex) { 返回 Json(ex.DataRecordInfo, JsonRequestBehavior.AllowGet); }

型号:

public class ForecastItem
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

    [Required( ErrorMessage = "Bitte ein Datum auswählen." )]
    [Display( Name = "Datum" )]
    public DateTime Date { get; set; }

    [Required( ErrorMessage = "Bitte ein Projekt auswählen." )]
    [Display( Name = "Projekt-ID" )]
    public int ProjectId { get; set; }

    [Required( ErrorMessage = "Bitte einen Preis eingeben." )]
    [Display( Name = "Preis" )]
    public decimal Price { get; set; }

    [Required( ErrorMessage = "Bitte einen Tickettyp hinterlegen." )]
    [Display( Name = "Tickettyp" )]
    public string TicketType { get; set; }

    [Required( ErrorMessage = "Bitte einen Abrechnungstyp auswählen." )]
    [Display( Name = "Abrechnungstyp" )]
    public string AccountingType { get; set; }

    [Required(ErrorMessage = "Bitte ein Ziel-Projektvolumen angeben.")]
    [Display(Name = "Zielvolumen")]
    public int TargetVolume { get; set; }

    [Display(Name = "Projekt")]
    public virtual Project Project { get; set; }
}

方法:

public JsonResult addForecastItem(string newDate, int newProjectId, int newForecastVolume, string newTicketType, string newCalcType, decimal newPrice)
    {
        ForecastItem fi = new ForecastItem
        {
            AccountingType = newCalcType,
            Date = DateTime.Now,
            Price = newPrice,
            ProjectId = newProjectId,
            TargetVolume = newForecastVolume,
            TicketType = newTicketType
        };

        if ( ModelState.IsValid)
        {
            try
            {
                db.ForecastItems.Add(fi);
                db.SaveChanges();
            }
            catch (Exception e)
            {
                return Json(e.Message, JsonRequestBehavior.AllowGet);
            }
        }

        return Json(fi, JsonRequestBehavior.AllowGet);
    }

任何想法我在这里做错了吗? 模型有效,数据库从软件的其他部分插入。

【问题讨论】:

  • 刷新更新数据库表和c#代码之间的映射文件,并以数据库为源。因此,如果您向类添加了属性,那么您将无法刷新这些项目。最好向数据库添加新列,然后刷新以更新类。
  • @jarlh 你是对的,我已经改变了标签
  • @jdweng 感谢您的解释。但我不敢说,我不明白第二部分:你能详细说明一下吗?你的意思是“如果你在类中添加了属性”我的 ForecastItem-Class 的注释有问题吗?还是您指的是我的吸气剂和二传手?当我的目标是添加新的“行”时,添加列是什么意思?提前谢谢你。

标签: c# sql-server database entity-framework


【解决方案1】:

哦,快点,

错误是:ID 没有自动递增。 该表是通过 SQL 创建的(原型,而不是产品),ID 是一个普通的 INT。

在创建类时添加了 [Key]-Attribute,但没有新建数据库。

解决方案:

  1. 生产,推荐:通过代码优先迁移 - [https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/migrations/][1]

  2. 注意 - 将删除所有数据:当数据不相关时:删除数据库并让 DbContext 在启动时构建数据库

  3. 我的解决方案(未链接数据时):

    alter table ForecastItems drop Column ID --drops column ID
    alter table ForecastItems add ID INT IDENTITY(1,1) -- creates column ID with` Autoincrement enabled
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多