0

I'm trying to add several objects to the DB with EF ObjectContext without calling the SaveChanges() after each add. Problem is that only the last added object is committed to the DB.

using (var entities = new ModelEntities())
{
    foreach (service in services)
    {
       entities.Services.AddObject(service);
    }
    entities.SaveChanges();
}

When I call the SaveChanges() method directly after AddObject all objects are inserted but performance is affected. What am I doing wrong?

Thanks.

4

1 回答 1

1

尝试:

using (var entities = new ModelEntities())
{
    foreach (var s in services)
    {
       var service = s;
       entities.Services.AddObject(service);
    }
    entities.SaveChanges();
}
于 2013-09-08T11:44:29.517 回答