【问题标题】:A column ID occurred more than once in the specification列 ID 在规范中出现多次
【发布时间】:2012-03-21 23:14:09
【问题描述】:

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

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

编辑

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

堆栈跟踪:

[NullReferenceException: 对象引用未设置为 对象。] ASP._Page_Views_Inbox_Index_cshtml.Execute() 在 c:\开发\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(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76
System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter 编写器,对象实例)+222
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext,TextWriter 作家)+115
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext 上下文) +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 过滤器,ResultExecutingContext preContext,Func1 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 控制器上下文,字符串 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.c_DisplayClassb.b_5() +34
System.Web.Mvc.Async.c_DisplayClass1.b_0() +19
System.Web.Mvc.Async.c_DisplayClass81.<BeginSynchronous>b__7(IAsyncResult _) +10 System.Web.Mvc.Async.WrappedAsyncResult1.End() +62 System.Web.Mvc.c_DisplayClasse.b_d() +48 System.Web.Mvc.SecurityUtil.b_0(动作 f)+7 System.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 step, 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);

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

【问题讨论】:

  • 显示可能的内部异常和堆栈跟踪可能会有所帮助。
  • 我发布了内部异常 Slauma。至于那个话题,不,它没有。我在其他不同的地方使用相同的技术,例如我有一个马类,它有一个大坝和一个公牛,都是类型的马,他们根本不抱怨身份证。这对我来说似乎很奇怪。

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


【解决方案1】:

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

【讨论】:

  • 就是这样,非常感谢。遇到了另一个问题,但它更进一步。
  • 我在用户模型中有 2 个电子邮件字段。一旦我删除了那些页面呈现正常。
猜你喜欢
  • 1970-01-01
  • 2011-08-10
  • 1970-01-01
  • 2017-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-13
  • 1970-01-01
相关资源
最近更新 更多