1

on unitOfWork.Complete(); I get this error

System.Data.Entity.Infrastructure.DbUpdateConcurrencyException: 'Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded

I gave a look at this but with no results. Also this question does not help me a lot. What's the point? Where is the error?

Program

using (var unitOfWork = new UnitOfWork(new 
    var ultimaDateTimeElaborata = 
            unitOfWork.LogRegistrazioneLavorazioneMacchinaRepository.
            GetUltimaRegistrazioneLineaRilevata(l.IdLinea);
    [...]

            unitOfWork.LogRegistrazioneLavorazioneMacchinaRepository.Add(
            new LogRegistrazioneLavorazioneMacchina()
            {
                IdMacchina = "MB1_1",
                DataRegistrazione = DateTime.Now,
                IdLinea = 1,
                MetriLinLato= 1,
                TempoLavorazioneSecondi= 1,
                IdLavorazioneLinea ="LavorazioneProva",
                IdLavorazioneMacchina = "LMB_1_PP",
                StepLavorazioneLinea = 3,
            });
            unitOfWork.Complete();

    //  HERE I GET THE ERROR!!! 
    // LogRegistrazioneLavorazioneMacchinaRepository.unitOfWork 
    //contains the right data I want to save
    unitOfWork.Complete(); 
    [...]
}

UnitOfWork

public class UnitOfWork : IUnitOfWork
    {
        private readonly J2MSEntities _context;
        public ILogRegistrazioneLavorazioneMacchinaRepository 
        LogRegistrazioneLavorazioneMacchinaRepository { get; private set; }


        public UnitOfWork(J2MSEntities context)
        {
            _context = context;
            LogRegistrazioneLavorazioneMacchinaRepository(_context);
        }

        public int Complete()
        {
           return _context.SaveChanges();
        }

        public void Dispose()
        {
            _context.Dispose();
        }
    }
Massimo Variolo
  • 4,669
  • 6
  • 38
  • 64
  • 1
    stupid question, does any request that invokes the *Program* part is called somewhere else also? or does anything spawns another `UnitOfWork` class? as far as i know, it can happen if you have two `DbContext` that attempting to update the same item... the later `DbContext` will fail with concurrency error. – Bagus Tesa Nov 13 '17 at 11:57
  • @BagusTesa yes it does, but for read purpose only. By the way I updated the 'program' code. – Massimo Variolo Nov 13 '17 at 12:42

1 Answers1

2

Right now I've found a "solution": the table where I would like to write had one primary key with two attributes; now with only one primary key with one attribute I have no errors.

Why?

Massimo Variolo
  • 4,669
  • 6
  • 38
  • 64
  • You mean you have a clustered index on the primary key columns ? because there is only one primary key allowed per table – Thanigainathan Nov 13 '17 at 15:57
  • *I would like to write had one primary key with two attributes* -- this one, a bit weird. did you set a [composite/compound key](https://en.wikipedia.org/wiki/Compound_key)? Anyway, as i've stated before.. you should encounter [no error as long as you have no two conflicting `DbContext`](https://stackoverflow.com/a/22036004/4648586).. try to debug your program first.. step by step. – Bagus Tesa Nov 14 '17 at 00:54