Data insertions without my request EF6

Skernel 1 Reputation point
2021-03-16T08:58:45.467+00:00

My project was created on VS17, it is on the 4.6.1 framework. I use Entity Framework 6 in database first. We switched to VS19 and this is the first time I debugged the project on it.

My pc crashed and showed me a blue screen while I was debugging.
When I restarted the project I noticed that the list I had loaded during the debugging with EF6, and displayed in a datagrid, was reinserted (duplicated) in the database with new id. For each inserted item, the related objects were also duplicated in cascade with new ids, as if the entities were no longer tracked by Entity.
However, to query the database I have to instantiate a new data service and use its methods. No method has been developed to insert or modify all a list. The modification or insertion of my data is only done by selecting an id or an entity.
I don't understand how EF6 has done to insert these items, is it possible that it is because of VS19? An incompatibility with the framework or Entity? Is there a link with the blue screen?

Thanks for your answers.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,911 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,783 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
814 questions
0 comments No comments
{count} votes

6 answers

Sort by: Most helpful
  1. Hui Liu-MSFT 48,571 Reputation points Microsoft Vendor
    2021-03-17T09:36:13.36+00:00

    @Skernel
    I created a project in the 4.6.1framework of VS19 and used EF6 with no re-insert item problem. It may not be a framework or entity incompatibility issue.
    It may be database disconnection.When users use DbSet.Add,not only is the state of the root entity marked ”Added” but everything in the graph that the context was not previously aware of is marked ”Added” as well. Even though the developer aware that the object has an existing Id value, Entity Framework honors its EntityState (Added) and creates an Insert database command for the object, regardless of the existing Id. That’s to ensure Entity Framework will get back the Id value of the newly created object so it can set the value in the object instance. For more details ,you can refer to here.
    It may also be that users are assigning an object to another object instance instead of the context, Entity Framework considers this to be an unrecognized entity and its default behavior for unrecognized entities with no state is to mark them as Added.And the object will be inserted into the database when SaveChanges is called. For more details ,you can refer to here.
    For the blue screen problem, can you show me the details? (Here is a link with the blue screen: Blue Screen Data.Maybe it can help you.)

    0 comments No comments

  2. Skernel 1 Reputation point
    2021-03-17T13:33:40.25+00:00

    Hello @Hui Liu-MSFT , thank you for your answer.
    I had already studied EntityState, but I don't think my problem comes from there.
    To explain the context I am in:
    I have made it so that for any request, I have to instantiate a new object (which I call DataService).
    This DataService contains methods that open a connection, query or insert, return the data, and close the connection.
    This is my problem:
    Before the bug occurred, I send requested to databse and instantiated a list of the result.
    After the bug, all the items that my list contained were reinserted.
    But I developed absolutely no method to insert a list, and no method accepts a list as argument. And after testing my code, I still don't understand how EF6 managed to insert a whole list without developing the code. Do you think that a disconnection from the database could have done that?

    As for the blue screen, I don't have details. When I go to the event viewer, I just have an information telling me that the shutdown of the computer was not normal, but I already suspected it... Also, I don't know if it has a direct link with my bug on EF6 or if it's a coincidence.

    0 comments No comments

  3. Hui Liu-MSFT 48,571 Reputation points Microsoft Vendor
    2021-03-18T10:07:04.607+00:00

    Hi,@Skernel ,
    Based on your description, you have trouble in how to use ef6 to insert a list to the database currently.

    First of all, I need to state that we have to connect to the database if we want to insert data to database.

    Second, please check if the following steps are same with you.

    1. Create the Application :EF6Sample(.NET Framework 4.6.1).
    2. Install the Entity Framework NuGet package :select Project –> Manage NuGet Packages and install the EntityFramework(latest version6.4.4).
      3.Define a Model.
      Add a new Product class to the EF6Sample: public class Product
      {
      public int ProductId { get; set; }
      public string Name { get; set; }
              public int CategoryId { get; set; }  
              public virtual Category Category { get; set; }  
          }  
          public class Category  
          {  
              public Category()  
              {  
                  this.Products = new List<Product>();  
              }  
      
              public int CategoryId { get; set; }  
              public string Name { get; set; }  
      
              public virtual List<Product> Products { get; private set; }  
      }  
      

    Add Context:

    public class ProductContext : DbContext  
        {  
            public DbSet<Product> Products { get; set; }  
            public DbSet<Category> Categories { get; set; }  
        }  
    

    4.Insert Data.
    cs code:

    private ProductContext context = new ProductContext();  
    
    private void Button_Click(object sender, RoutedEventArgs e)  
            {  
    
                    List<Product> products = new List<Product>()  
                    {  
                        new Product{Name="shirt" ,CategoryId=6},  
                        new Product{Name="cup" ,CategoryId=7},  
    
                    };  
    
                    List<Category> categorys = new List<Category>()  
                    {  
                        new Category{Name="cloth",CategoryId=6},  
                        new Category{Name="fruit",CategoryId=7},  
                    };  
    
                    context.Categories.AddRange(categorys);  
                    context.Products.AddRange(products);  
    
                    context.SaveChanges();  
    
    
                this.categoryDataGrid.Items.Refresh();  
                this.productsDataGrid.Items.Refresh();  
            }  
    

    For the blue screen problem, it may not be the influence of the code, but the computer problem.

    0 comments No comments

  4. Skernel 1 Reputation point
    2021-03-18T14:41:28.553+00:00

    Hello @Hui Liu-MSFT ,
    I use EF6 with database first, my object classes are generated automatically by entity framework.
    For data insertion, I also use the method you shared.
    However, the list I am loading is read-only, I have not developed any method to insert this list in database. Entity Framework acted on its own.
    The version of Entity Framework I had installed was 6.2.0, I updated it, I don't know if this is the cause of the problem.


  5. Duane Arnold 3,211 Reputation points
    2021-03-18T17:42:39.67+00:00

    @Skernel

    You need to show some code here that contributed to the issue. I find it hard to believe EF acted on its own.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.