实体框架4.3 - 代码优先 - 更新列表属性 [英] Entity Framework 4.3 - Code First - Update List Property

查看:132
本文介绍了实体框架4.3 - 代码优先 - 更新列表属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为我之前的问题,我现在知道,EF不会自动保存整个实体的所有更改。如果我的实体有一个List&Foo>,我需要更新该列表并保存。但是怎么样我已经尝试了一些事情,但是我无法将列表保存到正确的位置。

As a follow-up to my earlier question, I now know that EF doesn't just save all of the changes of the entire entity for me automatically. If my entity has a List<Foo>, I need to update that list and save it. But how? I've tried a few things, but I can't get the list to save properly.

我在Application和CustomVariableGroup之间有多对多的关联。一个应用可以有一个或多个组,一个组可以属于一个或多个应用。我相信我已经正确配置了我的Code First实现,因为我看到DB中的多对多关联表。

I have a many-to-many association between Application and CustomVariableGroup. An app can have one or more groups, and a group can belong to one or more apps. I believe I have this set up correctly with my Code First implementation because I see the many-to-many association table in the DB.

底线是应用程序class有一个List< CustomVariableGroup>。我的简单情况是应用程序已经存在,现在用户选择了一个属于该应用程序的组。我想在DB中保存更改。

The bottom line is that the Application class has a List<CustomVariableGroup>. My simple case is that the app already exists, and now a user has selected a group to belong to the app. I want to save that change in the DB.

尝试#1

this.Database.Entry(application).State = System.Data.EntityState.Modified;
this.Database.SaveChanges();

结果:关联表仍然没有行。

Result: Association table still has no rows.

尝试#2

this.Database.Applications.Attach(application);
var entry = this.Database.Entry(application);
entry.CurrentValues.SetValues(application);
this.Database.SaveChanges();

结果:关联表仍然没有行。

Result: Association table still has no rows.

尝试#3

CustomVariableGroup group = application.CustomVariableGroups[0];
application.CustomVariableGroups.Clear();
application.CustomVariableGroups.Add(group);
this.Database.SaveChanges();

结果:关联表仍然没有行。

Result: Association table still has no rows.

我已经研究了很多,而且我尝试过的东西比我所看到的更多,我根本就不知道如何使用新的CustomVariableGroup来更新应用程序的列表。如何做?

I've researched quite a bit, and I've tried more things than I've shown, and I simply don't know how to update an Application's list with a new CustomVariableGroup. How should it be done?

编辑(解决方案)

尝试和错误,这似乎是工作。看来我需要从DB中获取对象,修改它们,然后保存。

After hours of trial and error, this seems to be working. It appears that I need to get the objects from the DB, modify them, then save them.

public void Save(Application application)
{
    Application appFromDb = this.Database.Applications.Single(
      x => x.Id == application.Id);
    CustomVariableGroup groupFromDb = this.Database.CustomVariableGroups.Single(
      x => x.Id == 1);
    appFromDb.CustomVariableGroups.Add(groupFromDb);
    this.Database.SaveChanges();
}


推荐答案

虽然我认为这有点一个黑客,它的作品。我发布这个,希望它帮助别人节省一整天的工作。

While I consider this a bit of a hack, it works. I'm posting this in the hopes that it helps someone else save an entire day's worth of work.

public void Save(Application incomingApp)
{
    if (incomingApp == null) { throw new ArgumentNullException("incomingApp"); }

    int[] groupIds = GetGroupIds(incomingApp);

    Application appToSave;

    if (incomingApp.IdForEf == 0)  // New app
    {
        appToSave = incomingApp;
        // Clear groups, otherwise new groups will be added to the groups table.
        appToSave.CustomVariableGroups.Clear();
        this.Database.Applications.Add(appToSave);                
    }
    else
    {
        appToSave = this.Database.Applications
                .Include(x => x.CustomVariableGroups)
                .Single(x => x.IdForEf == incomingApp.IdForEf);
    }

    AddGroupsToApp(groupIds, appToSave);
    this.Database.SaveChanges();
}

private void AddGroupsToApp(int[] groupIds, Application app)
{
    app.CustomVariableGroups.Clear();

    List<CustomVariableGroup> groupsFromDb2 =
        this.Database.CustomVariableGroups.Where(g => groupIds.Contains(g.IdForEf)).ToList();

    foreach (CustomVariableGroup group in groupsFromDb2)
    {
        app.CustomVariableGroups.Add(group);
    }
}

private static int[] GetGroupIds(Application application)
{
    int[] groupIds = new int[application.CustomVariableGroups.Count];

    int i = 0;
    foreach (CustomVariableGroup group in application.CustomVariableGroups)
    {
        groupIds[i] = group.IdForEf;
        i++;
    }

    return groupIds;
}

这篇关于实体框架4.3 - 代码优先 - 更新列表属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆