实体框架4.1 CodeFirst问题MVC
我在使用最新的 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.ICollection
1[App.Domain.DailyHours])”的参数“dailyHours”的无效条目'App.Web.Controllers.DailyHoursController'。字典包含类型为“System.Collections.Generic.List
1[App.Domain.HoursRemaining]”的值,但参数需要类型为“System.Collections.Generic.ICollection`1[App.域.DailyHours]'。参数名称:参数
如果我颠倒编辑和保存的顺序,我会得到相同的错误,但 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);
我根本无法解决这个问题。任何帮助将不胜感激。
谢谢
戴维
I have a very strange problem using the latest the EF 4.1 with CodeFirst which I think is comes from a one to one relationship I have.
The problem is weird in that I have two differet controllers with unrelated entities that allow me to list and save an ICollection of an entity.
http://localhost:51638/DailyHours/Edit/1 and
http://localhost:51638/HoursRemaining/Edit/1
Both work in isolation. For example I enter one first, edit and save (works). I then enter the second,edit and save and it doesn't work. The error I get is:
The parameters dictionary contains an invalid entry for parameter 'dailyHours' for method 'System.Web.Mvc.ActionResult Edit(Int32, System.Collections.Generic.ICollection
1[App.Domain.DailyHours])' in 'App.Web.Controllers.DailyHoursController'. The dictionary contains a value of type 'System.Collections.Generic.List
1[App.Domain.HoursRemaining]', but the parameter requires a value of type 'System.Collections.Generic.ICollection`1[App.Domain.DailyHours]'. Parameter name: parameters
If I reverse the order in which I edit and save I get the same error but with the DailyHours and HourRemaining in reverse.
The key aspects of the pocos are:
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 has no direct relationship to either of these:
public class DailyHours
{
[Key]
public int DailyHoursId { get; set; }
public virtual Sprint Sprint { get; set; }
}
I have added code in my context's OnModelCreating to establish the principle in the one to one relationship:
modelBuilder.Entity<Task>().HasOptional(h => h.HoursRemaining).WithRequired(t => t.Task);
I can't fix this problem at all. Any help would be greatly appreciated.
Thanks
Davy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这里回答了。 模型绑定两个或多个集合
谢谢
answered here. Model binding two or more collections
thanks