列ID在规范中出现多次

本文关键字:范中出 ID | 更新日期: 2023-09-27 18:28:40

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

现在我正在开发一个"简单"的消息系统,但当我转到该页面时,我得到了标题中所述的错误

编辑

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

堆栈跟踪:

[NullReferenceException:对象引用未设置为对象。]ASP_中的Page_Views_Inbox_Index_shtml.Execute()c: ''Development''MVC''DOCCL''Views''Inbox''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(WebPageContextpageContext,TextWriter编写器,WebPageRenderingBase startPage)+76
System.Web.Mvc.RazorView.RenderView(ViewContext-ViewContext,TextWriter编写器,对象实例)+222
System.Web.Mvc.BuildManagerCompiledView.RRender(ViewContextviewContext,TextWriter编写器)+115
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext上下文)+295 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContextcontrollerContext,ActionResult ActionResult)+13
System.Web.Mvc.<>c_显示类1c.b_19()+23 System.Web.Mvc.ControllerActionInvoker.IInvokeActionResultFilter(IResultFilterfilter,ResultExecutingContext preContext,Func 1 continuation) +242
System.Web.Mvc.<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b() +21 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList
1 filter,ActionResult ActionResult)+177
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContextcontrollerContext,字符串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(RequestContextrequestContext)+10
System.Web.Mvc.<>c_显示类b.b_5()+34
System.Web.Mvc.Async。<>c_显示类1.b_0()+19
System.Web.Mvc.Async。<>c_DisplayClass8 1.<BeginSynchronous>b__7(IAsyncResult _) +10 System.Web.Mvc.Async.WrappedAsyncResult 1.End()+62 System.Web.Mvc.<>c_DisplayClasse.b_d()+48
System.Web.Mvc.SecurityUtil.b
_0(操作f)+7System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(操作操作)+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步骤,Boolean和completedSynchronously)+178

内部异常:{"列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);

有人知道我为什么会犯那个错误吗?在我添加这些类之前,它运行良好。

列ID在规范中出现多次

在您的消息模型中,Id不可为null,但您的操作允许为null,EF无法在查询中的where子句中匹配这两种类型&抛出异常。