如何选择特定实体对象进行更新?

时间:2011-03-01 21:38:33

标签: asp.net linq-to-sql entity-framework entity-framework-4

我从数据库中的实体中提取所有对象

Dim dbConfig as New housingEntities
Dim update_query = (From p in dbConfig.Configs _
                    Select p)

然后,我想单独访问行并对它们执行更新...例如,如果我只需要第一行,我就可以这样:

update_query.First.timeValue = txtFRRSD.Text
dbConfig.SubmitChanges()

现在,我不知道如何对此进行编码,但这里是假的,我想做什么:

update_query.Item("FRRSD").timeValue = txtFRRSD.Text
update_query.Item("FRRCD").timeValue = txtFRRCD.Text
update_query.Item("SORSD").timeValue = txtSORSD.Text
update_query.Item("SORCD").timeValue = txtSORCD.Text
dbConfig.SubmitChanges()

有没有人知道这样做或类似的方法?

1 个答案:

答案 0 :(得分:2)

以下是C#中我将如何一次更新多个实体对象的一般示例。

public void UpdateWidgetEntities(List<WidgetEntity> newWidgets)
{
   WidgetEntities widgetDB = new WidgetEntities();
   var dbWidgets = (from w in widgetDB.WidgetTable
                   where newWidgets.Contains(w.WidgetID)
                select w).ToList();

   foreach(var dbWidget in dbWidgets)
   {
      foreach(var widget in newWidgets)
      {
         if(dbWidget.WidgetID = widget.WidgetID)
            dbWidget.WidgetValue = widget.WidgetValue;
      }
   }
   widgetDB.SaveChanges();

}