我们可以在 entity framework 中附加多个对象并保存一次吗

Can we attach more than one object in entity framework and do savechanges once

我正在使用 Visual Studio 2012Entity Framework 6.0 我正在做一些 CRUD 操作,但我现在面临一些挑战。

我在一个表格中输入多个条目,想一次更新或保存这些详细信息,所以我现在这样做:

创建对象并存储值,并通过一一附加并保存更改来添加/更新

但是我可以这样做吗,比如附加 class 的多个对象并最后执行 savechanges 我的意思是在 for 循环之后。那么是不是通过附加多个对象运行保存多个savechanges

示例:

for(int i=0; i < comingrequestdata.Count(); i++)
{
    ObjectExample obj = new ObjectExampole();

    obj.Value1 = comingrequestdata[i].Value1;
    obj.Value2 = comingrequestdata[i].Value2;
    obj.Value3 = comingrequestdata[i].Value3;

    context.ObjectExamples.Attach(obj);
}

context.SaveChanges();

我可以这样做吗?我有点困惑,请有人帮助我。

您可以尝试下图

.附加

if you have an entity which it already exists in the database, and you have constructed the entity manually, use .Attach.

.AddObject

If you have a brand new object, use .AddObject.

for(int i=0; i < comingrequestdata.Count(); i++)
{
    ObjectExample obj = new ObjectExampole();

    obj.Value1 = comingrequestdata[i].Value1;
    obj.Value2 = comingrequestdata[i].Value2;
    obj.Value3 = comingrequestdata[i].Value3;

    context.ObjectExamples.AddObject(obj);
}

context.SaveChanges();

如果您想将所有行添加为新行:

for(int i=0; i < comingrequestdata.Count(); i++)
{
    //ObjectExample obj = new ObjectExampole();
    context.ObjectExamples.Add( new ObjectExample {
        Value1 = comingrequestdata[i].Value1,
        Value2 = comingrequestdata[i].Value2,
        Value3 = comingrequestdata[i].Value3
    }

}
context.SaveChanges();

这是如果你想修改数据库中的现有值(你需要一个主键来标识一条记录,以便从数据库中获取它,修改它,然后将它保存回来):

或者您可以使用其中一个 Value1/Value2/Value3,前提是它们中的任何一个都可以唯一标识该记录。

for(int i=0; i < comingrequestdata.Count(); i++)
{
    ObjectExample obj = context.ObjectExamples.Where(o => o.ID == Value4); // this is the unique id, assuming it is called ID in the database and Value4 stores its value in your code

    obj.Value1 = comingrequestdata[i].Value1;
    obj.Value2 = comingrequestdata[i].Value2;
    obj.Value3 = comingrequestdata[i].Value3;
    context.ObjectExamples.Attach(obj);
    context.Entry(obj).State = EntityState.Modified;
}
context.SaveChanges();

添加新项和更新修改项并不像调用一个方法那么简单。您需要区分何时添加新项目或何时更新。但是,是的,您可以提交多个项目以进行更新或添加为新项目。例如,此方法可以帮助您同时进行实体的添加和更新:

public SomeEntity AddOrUpdate(SomeEntity item)
{
    var original = _context.SomeDbSet.Local.FirstOrDefault(p => p.Id == item.Id) 
                   ?? _context.SomeDbSet.FirstOrDefault(p => p.Id == item.Id);

    if (original != null) // Updating
    {
        var entry = _context.Entry(original);
        entry.CurrentValues.SetValues(item);
    }
    else
    {
         // New item
         item = _context.SomeDbSet.Add(item);
    }

    return item;
}

多次调用此方法后,对基于 DbContext 的对象调用 SaveChanges

感谢大家的回答,我也做过这样的事情 只需创建 table 的对象,我的意思是 class,例如:

EntityFrameworkEntities db = new EntityFrameworkEntities();

Classname obj = new Classname();
obj.updatefirstvalue = something;
obj.updatesecondvalue = something;

db.Entry(obj).State = EntityState.Modified;
db.SaveChanges();

就是这样。我们还可以添加多达 class 的内容,我们希望最后使用 savechanges 进行更新,例如:

 foreach(var item in collection){
    Classname obj = new Classname();
    obj1.updatefirstvalue = item.something;
    obj1.updatesecondvalue = item.something;

    db.Entry(obj).State = EntityState.Modified;
    }
 db.SaveChanges();