ASP.Net 核心不断抱怨“数据库操作预计会影响 1 行,但实际上影响了 0 行”

我遇到了上述错误,不知道该怎么办。

您可以假设表已正确设置。这是问题代码:

    var setting = _context.Settings.FirstOrDefault();
    ICollection<PD_Datafile> PD_Datafiles = new Collection<PD_Datafile>();

    for (int i = 0; i < numfiles; i++)
    {
        DAT_FILE PD_Datafile = ParseFileName(files[i].Name, filepath);
        string databodystr = Encoding.Default.GetString(PD_Datafile.SensorData.DataBody);
        PD_Datafiles.Add(new PD_Datafile() { DataId = i, FullName = files[i].Name, DataBody = databodystr });
    }

    setting.PD_Datafiles = PD_Datafiles;

    ICollection<Alarm> alarms = new Collection<Alarm>();
    alarms.Add(new Alarm() { AlarmId = 0, DateOccurred = 20210120, EquipmentType = "GISPD", FaultType = "PD Happening", Sensor = null, Details = "None" });
    alarms.Add(new Alarm() { AlarmId = 1, DateOccurred = 20210121, EquipmentType = "CBCM", FaultType = "Warning", Sensor = null, Details = "None" });
    alarms.Add(new Alarm() { AlarmId = 2, DateOccurred = 20210122, EquipmentType = "DGA", FaultType = "Danger", Sensor = null, Details = "None" });
    alarms.Add(new Alarm() { AlarmId = 3, DateOccurred = 20210123, EquipmentType = "OLTC", FaultType = "Danger", Sensor = null, Details = "None" });

    setting.Alarms = alarms;
    _context.Settings.Update(setting);

    _context.SaveChanges();

当 '_context' 保存更改时,它一直说“'数据库操作预计会影响 1 行,但实际上影响了 0 行。自从加载实体以来,数据可能已被修改或删除。”

经过一番谷歌搜索后,我也在每个表的主键上方添加了这个符号。

[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]

我想我以某种错误的方式添加了列表。

以下是我正在使用的表格:

namespace OA2.Model
{
    public class Alarm
    {
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int AlarmId { get; set; }
        public string FaultType { get; set; }
        public int DateOccurred { get; set; }
        public Sensor Sensor { get; set; }
        public string EquipmentType { get; set; }
        public string Details { get; set; }
        [ForeignKey("SettingId")]
        public virtual Setting Setting { get; set; }
    }
}

namespace OA2.Model
{
    public partial class Setting
    {

        public Setting()
        {
            PD_Datafiles = new HashSet<PD_Datafile>();
            SCBR_Datafiles = new HashSet<SCBR_Datafile>();
            Alarms = new HashSet<Alarm>();
        }

        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int SettingId { get; set; }
        public int EventAmpTh1 { get; set; }
        public int EventPpsTh1 { get; set; }
        public int EventAmpTh2 { get; set; }
        public int EventPpsTh2 { get; set; }
        public int PulselvlTh { get; set; }
        public long TimePassed { get; set; }
        public string FilePath { get; set; }
        //public virtual PD_Datafile Data { get; set; }
        public virtual ICollection<PD_Datafile> PD_Datafiles { get; set; }
        public virtual ICollection<SCBR_Datafile> SCBR_Datafiles { get; set; }
        public virtual ICollection<Alarm> Alarms { get; set; }

    }
}

我在这里先向您的帮助表示感谢!

stack overflow ASP.Net core keeps complaining 'Database operation expected to affect 1 row(s) but actually affected 0 row(s)'
原文答案
author avatar

接受的答案

看:[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int AlarmId { get;放; }

这意味着数据库将生成AlarmId,因此您在创建新对象Alarm对象时无需分配AlarmId。

让我们删除 AlarmId = 0、AlarmId = 1 等...


答案:

作者头像

我认为警报桌上有问题。警报的 ID 是身份,因此您不需要添加警报 ID 第二件事是您同时修改两个表可能会出现异常。

相关问题