How can I solve it mistake? Store update, insert, or delete statement affected an unexpected number of rows (0).

Oğulcan Akca 196 Reputation points
2021-03-19T15:25:45.73+00:00

I want to delete all the information with the entered TC from the database, but I cannot. I can do the same for Id and delete it. How can I do this? I take the this fault;

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


OptimisticConcurrencyException: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information

public class EfEntityRepositoryBase<TEntity, TContext> : IEntityRepository<TEntity>
         where TEntity : class, IEntity, new()
        where TContext : DbContext, new()

    {
      public void Delete(TEntity entity)
        {
            using (TContext context = new TContext())
            {
                var deletedEntity = context.Entry(entity);
                deletedEntity.State = EntityState.Deleted;
                context.SaveChanges();
            }
        }

    }

public interface IEntityRepository<T> where T : class, IEntity, new()
    {

     void Delete(T entity);

    }

public interface IManagerPanelInformationDal :IEntityRepository<ManagerPanelInformation>
    {

    }
public class EfManagerPanelInformationDal : EfEntityRepositoryBase<ManagerPanelInformation, ManagerPanelContext>, IManagerPanelInformationDal
    {

    }

public class ManagerPanelInformationManager : IManagerPanelInformationService
    {
        public IManagerPanelInformationDal _managerPanelInformationDal;
        public ManagerPanelInformationManager()
        {

        }
 public void Delete(ManagerPanelInformation managerPanelInformation)
        {
            _managerPanelInformationDal.Delete(managerPanelInformation);
        }
    }

public interface IManagerPanelInformationService
    {
       void Update(ManagerPanelInformation managerPanelInformation);
    }

private void btnDelete_Click(object sender, EventArgs e)
        {


            if (radioButton1.Checked == true)

            {
                _iManagerPanelInformationService.Delete(new ManagerPanelInformation
                {
                    TC = Convert.ToInt64(tbxTCCC.Text)
                });

                MessageBox.Show("Tüm kişilerin bilgileri silindi!");
            }
            else
            {
                if (dgwRules.CurrentRow != null)
                {
                    try
                    {
                        _iManagerPanelInformationService.Delete(new ManagerPanelInformation
                        {
                            Id = Convert.ToInt32(dgwRules.CurrentRow.Cells[0].Value)
                        });
                        MessageBox.Show("Kişi bilgisi silindi!");

                    }
                    catch (Exception exception)
                    {
                        MessageBox.Show(exception.Message);
                    }

                }


                if (!String.IsNullOrEmpty(tbxTCCC.Text))
                {
                    dgwRules.DataSource = _iManagerPanelInformationService.GetManagerPanelInformationByTC(Convert.ToInt64(tbxTCCC.Text));

                }
            }
}

 public interface IEntity
    {

    }
public class ManagerPanelInformation : IEntity
    {
        public int Id { get; set; }
        public long TC { get; set; }
        public string Kurallar { get; set; }
    }

public class ManagerPanelContext : DbContext
    {
        public DbSet<ManagerPanelInformation> ManagerPanelInformations { get; set; }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            Database.SetInitializer<ManagerPanelContext>(null);
            base.OnModelCreating(modelBuilder);
        }

    }
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,417 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,312 questions
0 comments No comments
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,621 Reputation points
    2021-03-22T06:26:22.313+00:00

    Hi OulcanAkca-9475,
    The exception means that between time when you fetched data from database and modified it ,your data had been changed.
    More details you cna refer to this document.
    You can try use refresh method to fix it.

    try {  
        context.SaveChanges();  
    } catch (OptimisticConcurrencyException) {  
        context.Refresh(RefreshMode.ClientWins, yourentity);  
        context.SaveChanges();  
    }  
    

    On this Pronlem, here are more and solutions and suggestions.
    [Entity Framework: “Store update, insert, or delete statement affected an unexpected number of rows (0).” closed
    [Solution for: Store update, insert, or delete statement affected an unexpected number of rows (0) closed
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Duane Arnold 3,216 Reputation points
    2021-03-19T19:23:32.543+00:00

    Well first off, the Repository pattern is already being used by EF. So using the pattern over the pattern EF is already using is not an optimal approach.

    https://learn.microsoft.com/en-us/dotnet/api/system.data.entity.dbcontext?view=entity-framework-6.2.0

    The repository pattern is a domain pattern and not a data persistence pattern.

    https://martinfowler.com/eaaCatalog/repository.html

    https://www.infoworld.com/article/3117713/design-patterns-that-i-often-avoid-repository-pattern.html

    And the generic repository pattern does not lend itself to change and is inflexible.

    I like to use the DAO pattern with EF.

    https://javarevisited.blogspot.com/2013/01/data-access-object-dao-design-pattern-java-tutorial-example.html

    Here is an exmple of Windows form solution layered using DAO pattern in the DAL with EF out on Github.

    https://github.com/darnold924/PubComanyWinCore

    System.Data.Entity.Infrastructure.DbUpdateConcurrencyException: 'Store update, insert, or delete statement affected an unexpected number of rows (0).

    The error message means that the criteria used to tell EF to either update or delete records in database table or tables matched no table records, 0 records were affected and and nothing happened when something was expected to happen. So, EF threw the exception to let it be known that nothing happened.

    0 comments No comments