【问题标题】:how do I update an entity with a collection navigation property (with MVC2)如何使用集合导航属性更新实体(使用 MVC2)
【发布时间】:2010-11-08 23:57:40
【问题描述】:

我正在使用 MVC2 和 EF4 编写应用程序。我已经从我的数据库中创建了实体,我有一个 Timesheet 表和一个 TimesheetEntry 表。

我正在使用 MVC2 的模型绑定功能,并成功接收了一个 Timesheet 对象,其 TimesheetEntries 填充了用户创建的 TimesheetEntries。

如果我使用我在其他方法中使用过的技术(这里的时间表是 MVC 框架创建的对象):

Timesheet temp = context.Timesheets
                    .Include("TimesheetEntries")
                    .Where(t => t.Id == timesheet.Id).First();
context.ApplyCurrentValues<Timesheet>(temp.EntityKey.EntitySetName, timesheet);
context.SaveChanges();

那么我的 TimesheetEntries 没有保存。

我一直在尝试通过其他方式执行此操作但没有成功,例如删除所有条目:

Timesheet temp = context.Timesheets
       .Include("TimesheetEntries")
       .Where(t => t.Id == timesheet.Id).First();

context.ApplyCurrentValues<Timesheet>(temp.EntityKey.EntitySetName, timesheet);

context.ExecuteStoreCommand(@"DELETE FROM TimesheetEntry WHERE Timesheet=" + timesheet.Id.ToString());
foreach (TimesheetEntry entry in timesheet.TimesheetEntries)
{
    entry.Timesheet = timesheet.Id;
    context.TimesheetEntries.AddObject(entry);
}
context.SaveChanges();
return Redirect("ListTimesheets?PersonnelId="+timesheet.Person);

这不能正常工作,我仍然没有将 TimesheetEntries 输入数据库 :-( 只是想知道我是否在这里一起吠叫错误的树?

附:如果有人想了解有关这些事情的更多信息,请告诉我。我可以发布更多代码。

【问题讨论】:

    标签: c# asp.net-mvc-2 entity-framework-4


    【解决方案1】:

    同样,这里的时间表是由 MVC 映射创建的类

    Timesheet DBTimesheet = context.Timesheets
                        .Include("TimesheetEntries")
                        .Where(t => t.Id == timesheet.Id).First();
    
    //Delete if in database but not in submission
    DBTimesheet.TimesheetEntries
       .Where(dbE => !timesheet.TimesheetEntries.Any(e => e.Id == dbE.Id)).ToList()
       .ForEach(dbE => context.TimesheetEntries.DeleteObject(dbE));
    
    //Update if in submission and in database
    timesheet.TimesheetEntries
        .Where(e => DBTimesheet.TimesheetEntries.Any(dbE => dbE.Id == e.Id)).ToList()
        .ForEach(e => context.TimesheetEntries.ApplyCurrentValues(e));
    
    //Add if in submission but not in database
    timesheet.TimesheetEntries
        .Where(e => !DBTimesheet.TimesheetEntries.Any(dbE => dbE.Id == e.Id)).ToList()
        .ForEach(e => DBTimesheet.TimesheetEntries.Add(e));
    
    context.SaveChanges();
    

    根据需要更新、删除和添加引用的类,未完全测试

    【讨论】:

    • 我测试了它,它运行良好。对于 EF6,我必须将更新部分的 context.TimesheetEntries.ApplyCurrentValues(e) 更改为 ((IObjectContextAdapter)context).ObjectContext.ApplyCurrentValues("TimesheetEntries", e));
    【解决方案2】:

    阅读ApplyCurrentValues 的文档;它只做标量属性。

    但你可以这样做:

    Timesheet temp = context.Timesheets
                        .Include("TimesheetEntries")
                        .Where(t => t.Id == timesheet.Id).First();
    context.ApplyCurrentValues<Timesheet>(temp.EntityKey.EntitySetName, timesheet);
    foreach(var tse in timesheet.TimesheetEntries)
    {
        temp.TimesheetEntries.Add(tse); // or update, if need be.
    }
    context.SaveChanges();
    

    【讨论】:

    • 非常好。谢谢,我认为你在那里强调了一些非常好的东西。目前它并不完全有效,但我将尝试一些事情。关于阅读更多内容的好点,我将不得不这样做;-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多