【问题标题】:Entity Framework 4.1 CodeFirst Problem MVC实体框架 4.1 CodeFirst 问题 MVC
【发布时间】:2011-08-01 10:25:08
【问题描述】:

我在使用最新的 EF 4.1 和 CodeFirst 时遇到了一个非常奇怪的问题,我认为这是我所拥有的一对一关系。

问题很奇怪,因为我有两个具有不相关实体的不同控制器,它们允许我列出和保存实体的 ICollection。

http://localhost:51638/DailyHours/Edit/1 and

http://localhost:51638/HoursRemaining/Edit/1

两者都是独立工作的。例如我先输入一个,编辑并保存(作品)。然后我输入第二个,编辑并保存,它不起作用。我得到的错误是:

参数字典包含方法“System.Web.Mvc.ActionResult Edit(Int32, System.Collections.Generic.ICollection1[App.Domain.DailyHours])' in 'App.Web.Controllers.DailyHoursController'. The dictionary contains a value of type 'System.Collections.Generic.List1[App.Domain.HoursRemaining]”的参数“dailyHours”的无效条目,但是该参数需要一个类型为“System.Collections.Generic.ICollection`1[App.Domain.DailyHours]”的值。参数名称:parameters

如果我颠倒我编辑和保存的顺序,我会得到同样的错误,但 DailyHours 和 HourRemaining 是相反的。

pocos 的关键方面是:

public class Task
{
    [Key]
    public int TaskId { get; set; }
    public virtual HoursRemaining HoursRemaining { get; set; }
}

public class HoursRemaining
{
    [Key]
    public int TaskId { get; set; }
    public virtual Task Task { get; set; }
}

DailyHours 与以下任何一个都没有直接关系:

public class DailyHours
{                      
    [Key]
    public int DailyHoursId { get; set; }
    public virtual Sprint Sprint { get; set; }
}

我在上下文的 OnModelCreating 中添加了代码,以建立一对一关系中的原则:

modelBuilder.Entity<Task>().HasOptional(h => h.HoursRemaining).WithRequired(t => t.Task);

我根本无法解决这个问题。任何帮助将不胜感激。

谢谢

戴维

【问题讨论】:

    标签: model-view-controller frameworks entity code-first


    【解决方案1】:

    在这里回答。 Model binding two or more collections

    谢谢

    【讨论】:

      猜你喜欢
      • 2011-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-12
      • 1970-01-01
      • 2012-03-12
      • 2011-08-12
      • 1970-01-01
      相关资源
      最近更新 更多