skip to Main Content

I’m having the above error and don’t know what to do.

You can assume the tables are already set up correctly.
Here is the problem code:

    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();

When the ‘_context’ saves changes, it keeps saying "’Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded. "

After some googling, I’ve added this notation above the primary key of each tables as well.

[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]

I think I’m adding the list in the wrong way somehow.

Here are the tables that I’m working with:

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; }

    }
}

Thanks for the help in advance!

2

Answers


  1. I think problem on alarm table. Id for alarm is identity so you not need to add alarm id
    And
    Second thing is you are modifying two tables at same time may be this give exception.

    Login or Signup to reply.
  2. look at:
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int AlarmId { get; set; }

    it means the database will generate AlarmId, so you don’t need to assign AlarmId when creating new object Alarm object.

    let’s remove AlarmId = 0, AlarmId = 1, etc…

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search