简体   繁体   中英

Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified

Getting the below error while updating date fields of our Oracle table that has Composite Primary Key using Entity Framework. We checked all answers listed in the other thread ( Entity Framework: "Store update, insert, or delete statement affected an unexpected number of rows (0)." ) like entity state, id fields missing, and etc, but none of those helped. So creating new question to get fresh answer for this issue. Please note that we are using DevArt to connect to Oracle database.

"Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries"

Here is the code we are trying:

using (var context = new OracleContext(_connString))
{
    var dbObjT = context.ReportHistoryDatas.FirstOrDefault(x => x.key1.Trim() == "300041" && x.key2== 1);
    if(dbObjT != null)
    {
       dbObjT.DateUpdated = someDate;
       context.SaveChanges();
    }
}

Report History Data entity:

public class ReportHistoryData
{
    //PK ID
    public int key1 { get; set; }

    public string key2{ get; set; }

    public DateTime? DateAdded { get; set; }

    public DateTime? DateUpdated { get; set; }

    public class ReportHistoryDataConfiguration : EntityTypeConfiguration<ReportHistoryData>
    {
        public ReportHistoryDataConfiguration()
        {
            ToTable("REPORTHISTORY");
            HasKey(k => new {k.key1, k.key2});
            Property(p => p.key1).HasColumnName("key1").IsRequired();
            Property(p => p.key2).HasColumnName("key2").IsRequired();
            Property(p => p.DateAdded).HasColumnName("Date_Added");
            Property(p => p.DateUpdated).HasColumnName("Date_Updated");
        }
    }
}

I think you are missing to mark the entity as modified:

Try this:

using (var context = new OracleContext(_connString))
{
    var dbObjT = context.ReportHistoryDatas.FirstOrDefault(x => x.key1.Trim() == "300041" && x.key2== 1);
    if(dbObjT != null)
    {
       dbObjT.DateUpdated = someDate;

       //Mark the entity as modified before saving changes.
       context.Entry(dbObjT).State = System.Data.Entity.EntityState.Modified;

       context.SaveChanges();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM