列ID在规范中出现多次

时间:2012-03-21 23:14:10

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

最近我再次拿起了我的EF 4.1 / MVC 3项目并开始构建实际的前端功能。

现在我正在开发一个“简单”的消息系统,但在转到该页面后,我收到标题中所述的错误

修改

它创建数据库而不是模型。

堆栈追踪:

  

[NullReferenceException:对象引用未设置为的实例   对象。] ASP._Page_Views_Inbox_Index_cshtml.Execute()in   C:\开发\ MVC \ DOCCL \查看\收件箱\ Index.cshtml:18
  System.Web.WebPages.WebPageBase.ExecutePageHierarchy()+197
  System.Web.Mvc.WebViewPage.ExecutePageHierarchy()+81
  System.Web.WebPages.StartPage.RunPage()+17
  System.Web.WebPages.StartPage.ExecutePageHierarchy()+62
  System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext   pageContext,TextWriter writer,WebPageRenderingBase startPage)+76
  System.Web.Mvc.RazorView.RenderView(ViewContext viewContext,   TextWriter writer,Object instance)+222
  System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext   viewContext,TextWriter writer)+115
  System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)   +295 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext   controllerContext,ActionResult actionResult)+13
  System.Web.Mvc<> C_ DisplayClass1c.b _19()   +23 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter   filter,ResultExecutingContext preContext,Func 1 continuation) +242
System.Web.Mvc.<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b() +21 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList
1个过滤器,ActionResult actionResult)+177
  System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext   controllerContext,String actionName)+324
  System.Web.Mvc.Controller.ExecuteCore()+ 106   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)   +91 System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext   requestContext)+10
  System.Web.Mvc。&lt;&gt; c_ DisplayClassb.b _5()+34
  System.Web.Mvc.Async。&lt;&gt; c_ DisplayClass1.b _0()+19
  System.Web.Mvc.Async。&lt;&gt; c_ DisplayClass8 1.<BeginSynchronous>b__7(IAsyncResult _) +10 System.Web.Mvc.Async.WrappedAsyncResult 1.End()+62 System.Web.Mvc。&lt;&gt; c _DisplayClasse.b_ d()+48
  System.Web.Mvc.SecurityUtil.b
_0(动作f)+7   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action)   +22 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)+60
  System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult的   结果)+9
  System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()   +9478661 System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean&amp; completedSynchronously)+178

InnerException: {“列ID在规范中出现过多次。”}

最近添加的代码是。

控制器:

//
// GET: /Inbox/Index/5/1
public ActionResult Index(int? Id, int Page = 1)
{
    try
    {
        const int pageSize = 10;
        var messages = from m in horseTracker.Messages
                        where m.ReceiverId.Equals(Id)
                        select m;

        var paginatedMessages = new PaginatedList<Message>(messages, Page, pageSize);

        return View(paginatedMessages);
    }
    catch (Exception ex)
    {

    }
    return View();
}

模型

public class Message
{
    [Key]
    public int Id { get; set; }

    [Required(ErrorMessage = "Subject is required")]
    [Display(Name = "Subject")]
    public string Subject { get; set; }

    [Required(ErrorMessage = "Message is required")]
    [Display(Name = "Message")]
    public string Content { get; set; }

    [Required]
    [Display(Name = "Date")]
    public DateTime Created { get; set; }

    public Boolean Read { get; set; }

    [Required(ErrorMessage = "Can't create a message without a user")]
    public int SenderId { get; set; }
    public virtual User Sender { get; set; }

    [Required(ErrorMessage = "Please pick a recipient")]
    public int ReceiverId { get; set; }
    public virtual User Receiver { get; set; }
}

public class User
{
    [Key]
    public int Id { get; set; }

    [Required]
    [Display(Name = "Username")]
    public string UserName { get; set; }

    [Required]
    [Display(Name = "First Name")]
    public string FirstName { get; set; }

    [Required]
    [Display(Name = "Last Name")]
    public string LastName { get; set; }

    [Required]
    [Display(Name = "E-Mail")]
    public string Email { get; set; }

    [Required]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [Required]
    [Display(Name = "Country")]
    public string Country { get; set; }

    public string EMail { get; set; }

    //Races
    public virtual ICollection<Message> Messages { get; set; }
}

modelBuilder.Entity<User>()
    .HasMany(u => u.Messages)
    .WithRequired(m => m.Receiver)
    .HasForeignKey(m => m.ReceiverId)
    .WillCascadeOnDelete(false);

任何人都知道为什么我会收到这个错误? 在我添加这些类之前,它工作正常。

1 个答案:

答案 0 :(得分:1)

在您的消息模型中,Id不可为空,但您的操作允许空值,EF无法在查询和放大器的where子句中匹配这两种类型。抛出一个例外。