1

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");
        }
    }
}
Community
  • 1
  • 1
RAJA.P.N
  • 106
  • 8
  • You'll have to show your code for this. More than likely, you're using an object that gets modified and you'll have to create a copy of the object at some certain state before moving forward (so it can't get modified by another thread while you're working on it) -- or lock it. Can't tell without knowing what you're trying to do and how you're doing it. – Shannon Holsinger Sep 14 '16 at 17:22
  • Sorry updated the question with the code we are trying. Only one thread is running and we dont have any clue what is updating in b/w load and update. – RAJA.P.N Sep 14 '16 at 17:37
  • Are you sure you are retrieving a record and not an empty ReportContractHistoryDatas? First and foremost, I would do a null check since you are using FirstOrDefault. Alternatively, you can use First() which will throw an exception if there is not exactly one match in the DB. Do you have an entity definition for ReportContractHistoryDatas? Can you post that if you do? – JSF Sep 14 '16 at 17:50
  • Yes we get the record and we do check for null since its public forum i posted sample code. Anyway I will update the my question with null check. – RAJA.P.N Sep 14 '16 at 17:54
  • I posed ReportHistoryData entity definition for your reference. – RAJA.P.N Sep 14 '16 at 18:08
  • Are there any update triggers involved? – Gert Arnold Sep 14 '16 at 20:56
  • No triggers involved. – RAJA.P.N Sep 14 '16 at 21:47

1 Answers1

0

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();
    }
}
Karel Tamayo
  • 3,690
  • 2
  • 24
  • 30