Topic
  • 5 replies
  • Latest Post - ‏2019-05-06T07:44:52Z by Kappas
Carlos_Merida
Carlos_Merida
14 Posts

Pinned topic SaveChangesAsync function always return Concurrency Exception

‏2018-09-28T09:49:57Z |

Hi,

I get this error when use "await _context.SaveChangesAsync();":

DbUpdateConcurrencyException: 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. See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions.

My context configuration is:

services.AddDbContext<MyContext>(options =>
                options.UseDb2(Configuration.GetConnectionString("WebPlayMayoralCfgContext"),
                p =>
                {
                    p.SetServerInfo(IBMDBServerType.AS400, IBMDBServerVersion.AS400_07_02);
                    p.UseRowNumberForPaging();
                })
            );

Can anyone help me with this?

Regards,

Carlos.

  • RadhikaG
    RadhikaG
    12 Posts

    Re: SaveChangesAsync function always return Concurrency Exception

    ‏2018-10-10T06:46:16Z  

    Hi Carlos,

    Can you please send us the repro, we are unable to repro  it in-house!

    Also, please provide the Core Version details.

    Thanks,

    Radhika.

     

  • Carlos_Merida
    Carlos_Merida
    14 Posts

    Re: SaveChangesAsync function always return Concurrency Exception

    ‏2018-10-10T07:02:34Z  
    • RadhikaG
    • ‏2018-10-10T06:46:16Z

    Hi Carlos,

    Can you please send us the repro, we are unable to repro  it in-house!

    Also, please provide the Core Version details.

    Thanks,

    Radhika.

     

    Hi Radhika,

    My Core version is 2.1.

    I got this error with the create scaffolding action of any model. The scaffold use "await _context.SaveChangesAsync();" to insert the entity in the database, and I got this error. I have replaced for _context.SaveChanges() and worked, but I think is not normal the error with Async version.

    Regards,

    Carlos.

     

  • Kappas
    Kappas
    23 Posts

    Re: SaveChangesAsync function always return Concurrency Exception

    ‏2018-10-15T05:43:11Z  

    Hi,

     

    I got same kind of behaviour when mapping to existing physical file created by DDS. Its primary key is defined in related logical file and SQL world cannot see those via SQE. It was not an option for me to add those primary keys as those physical/logical files are generated via tool.

     

    When I created similar table via SQL's DDL, everything worked as intended, SaveChangesAsync() works without errors when inserting new rows.

     

    As a workaround for some "tables" we use good old SaveChanges when needed.

     

    Br,

    Kappas

     

    ps. Our version is 7.1, but I think you get same kind of behaviour with newer version of IBM i too

     

  • SundaySundaySunday
    SundaySundaySunday
    6 Posts

    Re: SaveChangesAsync function always return Concurrency Exception

    ‏2019-04-12T18:45:45Z  

    We are getting this error as well using v1.3.0.100 of IBM.Data.DB2.Core and IBM.EntityFrameworkCore.

    Journaling is enabled which fixes this issue http://www-01.ibm.com/support/docview.wss?uid=swg21676715

     

    SaveChanges() just works.

     

    To get SaveChangesAsync() to work I have to:

    Add Timestamp column as generated on row change to table.

    Add a DateTime property (I'll call it "MyConcurrencyProperty")

    In the DbContext.OnModelCreating() method: modelBuilder.Entity<MyTableClass>.Property(e => e.MyConcurrencyProperty).IsRowVersion()

    (Even then, I still run into issues here and there which appear to be concurrency related)

     

    AspNetCore Identity refuses to work out of the box because of its heavy reliance on SaveChangesAsync()

     

    Will this be fixed in the next release of the NuGet packages?

  • Kappas
    Kappas
    23 Posts

    Re: SaveChangesAsync function always return Concurrency Exception

    ‏2019-05-06T07:44:52Z  

    Hey,

     

    I noticed that in ef core tries to save in batches by default.

     

    So changing following setting

    .UseDb2(configuration["xxx"],
                       p =>
                       {
                           p.SetServerInfo(IBMDBServerType.AS400, IBMDBServerVersion.AS400_07_01);
                           p.UseRowNumberForPaging();
                           p.MaxBatchSize(1);
                       }
                   )

    I got concurrence errors go away.

    Doesnt sound performant solution but it works as quick fix

     

    /Kappas