Inserting multiple rows into a table using Entity Framework

前端 未结 3 572
[愿得一人]
[愿得一人] 2021-01-20 08:15

I’m having fun with EF and have come unstuck.

Originally I used the following bit of code using standard linq that essentially enters some data into a table.

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-20 09:00

    I'm using entity framework 5 now.
    When you use:

    objectcontext.Add(yourobject1);
    objectcontext.Add(yourobject2);
    objectcontext.SaveChanges();
    

    then only last of your objects will be inserted into database.

    But if you will use:

    objectcontext.Entry(yourobject1).State = System.Data.EntityState.Added;
    objectcontext.Entry(yourobject2).State = System.Data.EntityState.Added;
    objectcontext.SaveChanges();
    

    Then all of your objects will be added.

提交回复
热议问题