简体   繁体   English

实体框架4.3-代码优先-更新列表属性

[英]Entity Framework 4.3 - Code First - Update List Property

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. 作为我之前的问题的后续内容,我现在知道EF不仅会自动为我保存整个实体的所有更改。 If my entity has a List<Foo>, I need to update that list and save it. 如果我的实体具有List <Foo>,则需要更新该列表并保存。 But how? 但是如何? I've tried a few things, but I can't get the list to save properly. 我已经尝试了一些方法,但是无法正确保存列表。

I have a many-to-many association between Application and CustomVariableGroup. 我在Application和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. 我相信我的Code First实现可以正确设置此设置,因为我在数据库中看到了多对多关联表。

The bottom line is that the Application class has a List<CustomVariableGroup>. 最重要的是,Application类具有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. 我想将该更改保存在数据库中。

Attempt #1 尝试#1

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

Result: Association table still has no rows. 结果:关联表仍然没有行。

Attempt #2 尝试#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. 结果:关联表仍然没有行。

Attempt #3 尝试#3

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

Result: Association table still has no rows. 结果:关联表仍然没有行。

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. 我已经进行了很多研究,并且尝试了比所显示的更多的事情,而且我根本不知道如何使用新的CustomVariableGroup更新应用程序的列表。 How should it be done? 应该怎么做?

EDIT (Solution) 编辑(解决方案)

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM