Search code examples
c#visual-studioentity-framework-6ef-code-first.net-4.8

An entity object cannot be referenced by multiple instances of IEntityChangeTracker using code-first and EntityFramework 6 ,WFA


When i try to change the data in the database, an exception is thrown. I am new to Entity and don't undentend how to fix it. I use MS SQL Server. using code-first and EntityFramework 6 ,WFA.

Method

private void editCircularToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Circular c = DG_C.CurrentRow.DataBoundItem as Circular;
            MyContext context = new MyContext();
            Red_Circular red = context.Reds.SingleOrDefault(r => r.Circular__ID == c.ID_Circular);
            AddCircular add = new AddCircular();
            add.tbsur.Text = c.Surname;
            add.tbname.Text = c.Name;
            add.cbeye.Text = c.EyeColor;                           // adding values to the textboxes on anoter forms
            add.cbhair.Text = c.HairColor;                         // добавление значение в тексбоксы другой формы

            if (red != null)// if found если найдено
            {
                add.tbchange.Text = red.charge;
                DialogResult result = add.ShowDialog(this);
                if (result == DialogResult.Cancel)
                {
                    c.Surname = add.tbsur.Text;
                    c.Name = add.tbname.Text;

                    c.EyeColor = add.cbeye.Text;
                    c.HairColor = add.cbhair.Text; // adding new values добавления новых значений
                    c.Activity = true;
                    red.charge = add.tbchange.Text;
                    red.info = add.tbinfo.Text;
                    context.Entry(c).State = EntityState.Modified; // error ошибка
                    //An entity object cannot be referenced by multiple instances of IEntityChangeTracker
                    context.Entry(red).State = EntityState.Modified;// if you delete 
                    //the line abode then this line works

                    context.SaveChanges();

                }
}

Class Context

public class MyContext : DbContext
    {
        public MyContext() : base("DefaultConnection")
        {

        }
        public DbSet<Department> Departments { get; set; }
        public DbSet<User> Users { get; set; }
        public DbSet<Resualt_of_search> Resualts { get; set; }
        public DbSet<Employee> Employees { get; set; }
        public DbSet<Circular> Circulars { get; set; }
        public DbSet<Red_Circular> Reds { get; set; }
        public DbSet<Blue_Circular> Blues { get; set; }
        public DbSet<Black_Circular> Blacks { get; set; }
        public DbSet<Yellow_circular> Yellows { get; set; }
        public DbSet<Green_Circular> Greens { get; set; }
    }

Solution

  • it seems like you get your Circular c = DG_C.CurrentRow.DataBoundItem as Circular; from some kind of a datagrid that's filled using a diffrent context than the one you use the get this Red_Circular red = context.Reds.SingleOrDefault(r => r.Circular__ID == c.ID_Circular);

    try to use one global context in your class and then use that to fill your grid and get your Red_Circular

    that sould solve it