我有一个简单的查询,可以将两个表中的数据加载到 GUI 中。我正在将加载的数据保存到广泛可用的对象 Clients currentlySelectedClient

using (var context = new EntityBazaCRM())
{
   currentlySelectedClient = context.Kliencis.Include("Podmioty").FirstOrDefault(d => d.KlienciID == klientId);
   if (currentlySelectedClient != null)
   {
      textImie.Text = currentlySelectedClient.Podmioty.PodmiotOsobaImie;
      textNazwisko.Text = currentlySelectedClient.Podmioty.PodmiotOsobaNazwisko;
   }
   else
   {
      textNazwa.Text = currentlySelectedClient.Podmioty.PodmiotFirmaNazwa;
   }
}

所以现在如果我想:

1) 保存用户所做的更改我该怎么做?我是否必须在数据库端准备一些东西?我如何处理修改多个表(有些数据在这里,有些数据在那里)?我当前的代码似乎写 .KlienciHaslo 就好了,但它根本不影响 Podmioty。我尝试了不同的组合,但没有运气。

2)将新客户端添加到数据库(并将信息也保存到相关表中)?
    currentClient.Podmioty.PodmiotOsobaImie = textImie.Text;  // not saved
    currentClient.Podmioty.PodmiotOsobaNazwisko = textNazwisko.Text; // not saved
    currentClient.KlienciHaslo = "TEST111"; // saved

    using (var context = new EntityBazaCRM())
    {
        var objectInDB = context.Kliencis.SingleOrDefault(t => t.KlienciID == currentClient.KlienciID);
        if (objectInDB != null)
        {
           // context.ObjectStateManager.ChangeObjectState(currentClient.Podmioty, EntityState.Modified);
           //context.Podmioties.Attach(currentClient.Podmioty);
           context.Kliencis.ApplyCurrentValues(currentClient); // update current client
           //context.ApplyCurrentValues("Podmioty", currentClient.Podmioty); // update current client
        }
        else
        {
           context.Kliencis.AddObject(currentClient);  // save new Client
        }
        context.SaveChanges();
     }

我怎样才能做到这两点?

编辑答案(不保存任何内容):
currentClient.Podmioty.PodmiotOsobaImie = textImie.Text; // no save
currentClient.Podmioty.PodmiotOsobaNazwisko = textNazwisko.Text; // no save
currentClient.KlienciHaslo = "TEST1134"; // no save

using (var context = new EntityBazaCRM())
{
    if (context.Kliencis.Any(t => t.KlienciID == currentClient.KlienciID))
    {
        context.Kliencis.Attach(currentClient); // update current client
    }
    else
    {
        context.Kliencis.AddObject(currentClient);  // save new Client
    }
    context.SaveChanges();
}

最佳答案

显然 ApplyCurrentValues 只适用于标量属性。

如果您附加了 currentClient,那么关联的对象也应该附加,这意味着它们会在您附加 SaveChanges() 时更新

但是 你会得到一个 Object with the key exists 异常,因为你已经将对象从数据库加载到 objectInDB 变量中。上下文只能包含一个实体的副本,并且它知道 currentClientobjectInDB 相同,因此抛出异常。

试试这个模式

if (context.Kliencis.Any(t => t.KlienciID == currentClient.KlienciID))
{
    context.Kliencis.Attach(currentClient); // update current client
}
else
{
    context.Kliencis.AddObject(currentClient);  // save new Client
}

或者,如果您使用身份作为 ID,则
// if the ID is != 0 then it's an existing database record
if (currentClient.KlienciID != 0)
{
    context.Kliencis.Attach(currentClient); // update current client
}
else // the ID is 0; it's a new record
{
    context.Kliencis.AddObject(currentClient);  // save new Client
}

关于c# - 使用 Entity Framework 将更改保存回数据库,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8295188/

10-17 02:37
查看更多