我正在开发一个ASP.NET MVC应用程序,最近,我的数据库中的一个表不再显示其外键,我不能为我的生活找出原因。数据库中有其他表引用与此相同的表,并且它们工作正常,但由于某种原因,现在显示为空白。其他表使用非常相似的代码,这就是为什么我特别难过。我甚至尝试删除并重新构建控制器和视图,并在将@Html.DisplayFor(modelItem => item.Ref)
更改为类似@Html.DisplayFor(modelItem => item.Product.ProductID)
之类的内容时产生同样的问题。
编辑:
我首先使用EF代码,遗憾的是重新播种我的数据库并没有帮助。如果我展示我的上下文类会有帮助吗?
第二次编辑:
我解决了这个问题,在我的上下文类中,我将每个外键声明为可选,这出于某种原因意味着它们根本没有显示。删除后,现在一切正常。
控制器代码:
public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
{
ViewBag.CurrentSort = sortOrder;
ViewBag.IDSortParam = String.IsNullOrEmpty(sortOrder) ? "ID_desc" : "";
ViewBag.RefSortParam = sortOrder == "Ref" ? "Ref_desc" : "Ref";
ViewBag.ProdNameSortParam = sortOrder == "ProdName" ? "ProdName_desc" : "ProdName";
ViewBag.UnitPriceSortParam = sortOrder == "UnitPrice" ? "UnitPrice_desc" : "UnitPrice";
ViewBag.FormatSortParam = sortOrder == "Format" ? "Format_desc" : "Format";
ViewBag.QuantitySortParam = sortOrder == "Quantity" ? "Quantity_desc" : "Quantity";
ViewBag.AvailabilitySortParam = sortOrder == "Availability" ? "Availability_desc" : "Availability";
ViewBag.NewSortParam = sortOrder == "New" ? "New_desc" : "New";
ViewBag.LocationSortParam = sortOrder == "Location" ? "Location_desc" : "Location";
ViewBag.OrigNoSortParam = sortOrder == "OrigNo" ? "OrigNo_desc" : "OrigNo";
ViewBag.TitleSortParam = sortOrder == "Title" ? "Title_desc" : "Title";
ViewBag.ShortTitleSortParam = sortOrder == "ShortTitle" ? "ShortTitle_desc" : "ShortTitle";
ViewBag.LinkSortParam = sortOrder == "Link" ? "Link_desc" : "Link";
ViewBag.NotesSortParam = sortOrder == "Notes" ? "Notes_desc" : "Notes";
ViewBag.Notes2SortParam = sortOrder == "Notes2" ? "Notes2_desc" : "Notes2";
ViewBag.TypeSortParam = sortOrder == "Type" ? "Type_desc" : "Type";
ViewBag.SerTitleSortParam = sortOrder == "SerTitle" ? "SerTitle_desc" : "SerTitle";
ViewBag.SubjectSortParam = sortOrder == "Subject" ? "Subject_desc" : "Subject";
ViewBag.ClassSubjSortParam = sortOrder == "ClassSubj" ? "ClassSubj_desc" : "ClassSubj";
ViewBag.ClassGeogSortParam = sortOrder == "ClassGeog" ? "ClassGeog_desc" : "ClassGeog";
ViewBag.ClassLangSortParam = sortOrder == "ClassLang" ? "ClassLang_desc" : "ClassLang";
ViewBag.ClassDateSortParam = sortOrder == "ClassDate" ? "ClassDate_desc" : "ClassDate";
ViewBag.RelName1SortParam = sortOrder == "RelName1" ? "RelName1_desc" : "RelName1";
ViewBag.RelName2SortParam = sortOrder == "RelName2" ? "RelName2_desc" : "RelName2";
ViewBag.RelName3SortParam = sortOrder == "RelName3" ? "RelName3_desc" : "RelName3";
ViewBag.RelName4SortParam = sortOrder == "RelName4" ? "RelName4_desc" : "RelName4";
ViewBag.RelTit1SortParam = sortOrder == "RelTit1" ? "RelTit1_desc" : "RelTit1";
ViewBag.RelTit2SortParam = sortOrder == "RelTit2" ? "RelTit2_desc" : "RelTit2";
ViewBag.RelTit3SortParam = sortOrder == "RelTit3" ? "RelTit3_desc" : "RelTit2";
if (searchString != null)
{
page = 1;
}
else
{
searchString = currentFilter;
}
ViewBag.CurrentFilter = searchString;
var catalogs = from c in db.Catalogs//.Include(c => c.BicFull).Include(c => c.BicFull2).Include(c => c.BicFull3).Include(c => c.BicFull4).Include(c => c.Product).Include(c => c.RelatedName).Include(c => c.RelatedName2).Include(c => c.RelatedName3).Include(c => c.RelatedName4).Include(c => c.RelatedTitle).Include(c => c.RelatedTitle2).Include(c => c.RelatedTitle3).Include(c => c.RelatedTitle4).Include(c => c.Series)
select c;
if (!String.IsNullOrEmpty(searchString))
{
catalogs = catalogs.Where(s => s.Ref.ToUpper().Contains(searchString.ToUpper()) ||
s.ProductID.ToUpper().Contains(searchString.ToUpper()) ||
s.Format.ToUpper().Contains(searchString.ToUpper()));
}
switch (sortOrder)
{
case "ID_desc":
catalogs = catalogs.OrderByDescending(s => s.CatalogID);
break;
case "Ref":
catalogs = catalogs.OrderBy(s => s.Ref);
break;
case "Ref_desc":
catalogs = catalogs.OrderByDescending(s => s.Ref);
break;
case "ProdName":
catalogs = catalogs.OrderBy(s => s.ProductID);
break;
case "ProdName_desc":
catalogs = catalogs.OrderByDescending(s => s.ProductID);
break;
case "UnitPrice":
catalogs = catalogs.OrderBy(s => s.UnitPrice);
break;
case "UnitPrice_desc":
catalogs = catalogs.OrderByDescending(s => s.UnitPrice);
break;
case "Format":
catalogs = catalogs.OrderBy(s => s.Format);
break;
case "Format_desc":
catalogs = catalogs.OrderByDescending(s => s.Format);
break;
case "Quantity":
catalogs = catalogs.OrderBy(s => s.Quantity);
break;
case "Quantity_desc":
catalogs = catalogs.OrderByDescending(s => s.Quantity);
break;
case "Availability":
catalogs = catalogs.OrderBy(s => s.Availability);
break;
case "Availability_desc":
catalogs = catalogs.OrderByDescending(s => s.Availability);
break;
case "New":
catalogs = catalogs.OrderBy(s => s.New);
break;
case "New_desc":
catalogs = catalogs.OrderByDescending(s => s.New);
break;
case "Location":
catalogs = catalogs.OrderBy(s => s.Location);
break;
case "Location_desc":
catalogs = catalogs.OrderByDescending(s => s.Location);
break;
case "OrigNo":
catalogs = catalogs.OrderBy(s => s.OriginalNo);
break;
case "OrigNo_desc":
catalogs = catalogs.OrderByDescending(s => s.OriginalNo);
break;
case "Title":
catalogs = catalogs.OrderBy(s => s.Title);
break;
case "Title_desc":
catalogs = catalogs.OrderByDescending(s => s.Title);
break;
case "ShortTitle":
catalogs = catalogs.OrderBy(s => s.ShortTitle);
break;
case "ShortTitle_desc":
catalogs = catalogs.OrderByDescending(s => s.ShortTitle);
break;
case "Link":
catalogs = catalogs.OrderBy(s => s.Link);
break;
case "Link_desc":
catalogs = catalogs.OrderByDescending(s => s.Link);
break;
case "Notes":
catalogs = catalogs.OrderBy(s => s.Notes);
break;
case "Notes_desc":
catalogs = catalogs.OrderByDescending(s => s.Notes);
break;
case "Notes2":
catalogs = catalogs.OrderBy(s => s.Notes2);
break;
case "Notes2_desc":
catalogs = catalogs.OrderByDescending(s => s.Notes2);
break;
case "Type":
catalogs = catalogs.OrderBy(s => s.Type);
break;
case "Type_desc":
catalogs = catalogs.OrderByDescending(s => s.Type);
break;
case "SerTitle":
catalogs = catalogs.OrderBy(s => s.SeriesID);
break;
case "SerTitle_desc":
catalogs = catalogs.OrderByDescending(s => s.SeriesID);
break;
case "Subject":
catalogs = catalogs.OrderBy(s => s.Subject);
break;
case "Subject_desc":
catalogs = catalogs.OrderByDescending(s => s.Subject);
break;
case "ClassSubj":
catalogs = catalogs.OrderBy(s => s.ClassSubj);
break;
case "ClassSubj_desc":
catalogs = catalogs.OrderByDescending(s => s.ClassSubj);
break;
case "ClassGeog":
catalogs = catalogs.OrderBy(s => s.ClassGeog);
break;
case "ClassGeog_desc":
catalogs = catalogs.OrderByDescending(s => s.ClassGeog);
break;
case "ClassLang":
catalogs = catalogs.OrderBy(s => s.ClassLang);
break;
case "ClassLang_desc":
catalogs = catalogs.OrderByDescending(s => s.ClassLang);
break;
case "ClassDate":
catalogs = catalogs.OrderBy(s => s.ClassDate);
break;
case "ClassDate_desc":
catalogs = catalogs.OrderByDescending(s => s.ClassDate);
break;
case "RelName1":
catalogs = catalogs.OrderBy(s => s.RelatedNames1);
break;
case "RelName1_desc":
catalogs = catalogs.OrderByDescending(s => s.RelatedNames1);
break;
case "RelName2":
catalogs = catalogs.OrderBy(s => s.RelatedNames2);
break;
case "RelName2_desc":
catalogs = catalogs.OrderByDescending(s => s.RelatedNames2);
break;
case "RelName3":
catalogs = catalogs.OrderBy(s => s.RelatedNames3);
break;
case "RelName3_desc":
catalogs = catalogs.OrderByDescending(s => s.RelatedNames3);
break;
case "RelName4":
catalogs = catalogs.OrderBy(s => s.RelatedNames4);
break;
case "RelName4_desc":
catalogs = catalogs.OrderByDescending(s => s.RelatedNames4);
break;
case "RelTit1":
catalogs = catalogs.OrderBy(s => s.RelatedTitles1);
break;
case "RelTit1_desc":
catalogs = catalogs.OrderByDescending(s => s.RelatedTitles1);
break;
case "RelTit2":
catalogs = catalogs.OrderBy(s => s.RelatedTitles2);
break;
case "RelTit2_desc":
catalogs = catalogs.OrderByDescending(s => s.RelatedTitles2);
break;
case "RelTit3":
catalogs = catalogs.OrderBy(s => s.RelatedTitles3);
break;
case "RelTit3_desc":
catalogs = catalogs.OrderByDescending(s => s.RelatedTitles3);
break;
default:
catalogs = catalogs.OrderBy(s => s.CatalogID);
break;
}
int pageSize = 10;
int pageNumber = (page ?? 1);
return View(catalogs.ToPagedList(pageNumber, pageSize));
目录模型:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Microform_Intranet.Models
{
public class Catalog
{
[Display(Name="Catalog ID")]
public int CatalogID { get; set; }
[ForeignKey("Product")]
public string Ref { get; set; }
[Display(Name="Product Name")]
[ForeignKey("Product2")]
public string ProductID { get; set; }
[Display(Name="Unit Price")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
public decimal? UnitPrice { get; set; }
public string Format { get; set; }
public string Quantity { get; set; }
public string Availability { get; set; }
public string New { get; set; }
public string Location { get; set; }
[Display(Name="Original No.")]
public string OriginalNo { get; set; }
public string Title { get; set; }
[ForeignKey("RelatedTitle4")]
public int? ShortTitle { get; set; }
public string Link { get; set; }
public string Notes { get; set; }
[Display(Name="Notes 2")]
public string Notes2 { get; set; }
public string Type { get; set; }
[Display(Name="Series Title")]
public int? SeriesID { get; set; }
public string Subject { get; set; }
[ForeignKey("BicFull")]
public string ClassSubj { get; set; }
[ForeignKey("BicFull2")]
public string ClassGeog { get; set; }
[ForeignKey("BicFull3")]
public string ClassLang { get; set; }
[ForeignKey("BicFull4")]
public string ClassDate { get; set; }
[ForeignKey("RelatedName")]
public int? RelatedNames1 { get; set; }
[ForeignKey("RelatedName2")]
public int? RelatedNames2 { get; set; }
[ForeignKey("RelatedName3")]
public int? RelatedNames3 { get; set; }
[ForeignKey("RelatedName4")]
public int? RelatedNames4 { get; set; }
[ForeignKey("RelatedTitle")]
public int? RelatedTitles1 { get; set; }
[ForeignKey("RelatedTitle2")]
public int? RelatedTitles2 { get; set; }
[ForeignKey("RelatedTitle3")]
public int? RelatedTitles3 { get; set; }
public virtual Product Product { get; set; }
public virtual Product Product2 { get; set; }
public virtual Series Series { get; set; }
public virtual BicFull BicFull { get; set; }
public virtual BicFull BicFull2 { get; set; }
public virtual BicFull BicFull3 { get; set; }
public virtual BicFull BicFull4 { get; set; }
public virtual RelatedName RelatedName { get; set; }
public virtual RelatedName RelatedName2 { get; set; }
public virtual RelatedName RelatedName3 { get; set; }
public virtual RelatedName RelatedName4 { get; set; }
public virtual RelatedTitle RelatedTitle { get; set; }
public virtual RelatedTitle RelatedTitle2 { get; set; }
public virtual RelatedTitle RelatedTitle3 { get; set; }
public virtual RelatedTitle RelatedTitle4 { get; set; }
}
}
目录索引视图:
@model PagedList.IPagedList<Microform_Intranet.Models.Catalog>
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
@{
ViewBag.Title = "Index";
}
<h2>Catalog</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
@using (Html.BeginForm("Index", "Catalog", FormMethod.Get))
{
<p>
Find by Ref, Product Name or Format: @Html.TextBox("searchString", ViewBag.CurrentFilter as string)
<input type="submit" value="Search" />
</p>
}
<div class="datatable" style="overflow-x:scroll">
<table class="table" style="table-layout:fixed">
<tr>
<th style="width:90px">
@Html.ActionLink("Catalog ID", "Index", new { sortOrder = ViewBag.IDSortParam, currentFilter = ViewBag.CurrentFilter })
</th>
<th style="width:125px">
@Html.ActionLink("Ref", "Index", new { sortOrder = ViewBag.RefSortParam, currentFilter = ViewBag.CurrentFilter })
</th>
<th style="width:300px">
@Html.ActionLink("Product Name", "Index", new { sortOrder = ViewBag.ProdNameSortParam, currentFilter = ViewBag.CurrentFilter })
</th>
<th style="width:100px">
@Html.ActionLink("Unit Price", "Index", new { sortOrder = ViewBag.UnitPriceSortParam, currentFilter = ViewBag.CurrentFilter })
</th>
<th style="width:150px">
@Html.ActionLink("Format", "Index", new { sortOrder = ViewBag.FormatSortParam, currentFilter = ViewBag.CurrentFilter })
</th>
<th style="width:75px">
@Html.ActionLink("Quantity", "Index", new { sortOrder = ViewBag.QuantitySortParam, currentFilter = ViewBag.CurrentFilter })
</th>
<th style="width:150px">
@Html.ActionLink("Availability", "Index", new { sortOrder = ViewBag.AvailabilitySortParam, currentFilter = ViewBag.CurrentFilter })
</th>
<th style="width:120px">
@Html.ActionLink("New", "Index", new { sortOrder = ViewBag.NewSortParam, currentFilter = ViewBag.CurrentFilter })
</th>
<th style="width:200px">
@Html.ActionLink("Location", "Index", new { sortOrder = ViewBag.LocationSortParam, currentFilter = ViewBag.CurrentFilter })
</th>
<th style="width:150px">
@Html.ActionLink("Original No.", "Index", new { sortOrder = ViewBag.OrigNoSortParam, currentFilter = ViewBag.CurrentFilter })
</th>
<th style="width:400px">
@Html.ActionLink("Title", "Index", new { sortOrder = ViewBag.TitleSortParam, currentFilter = ViewBag.CurrentFilter })
</th>
<th style="width:300px">
@Html.ActionLink("Short Title", "Index", new { sortOrder = ViewBag.ShortTitleSortParam, currentFilter = ViewBag.CurrentFilter })
</th>
<th style="width:350px">
@Html.ActionLink("Link", "Index", new { sortOrder = ViewBag.LinkSortParam, currentFilter = ViewBag.CurrentFilter })
</th>
<th style="width:1000px">
@Html.ActionLink("Notes", "Index", new { sortOrder = ViewBag.NotesSortParam, currentFilter = ViewBag.CurrentFilter })
</th>
<th style="width:350px">
@Html.ActionLink("Notes 2", "Index", new { sortOrder = ViewBag.Notes2SortParam, currentFilter = ViewBag.CurrentFilter })
</th>
<th style="width:150px">
@Html.ActionLink("Type", "Index", new { sortOrder = ViewBag.TypeSortParam, currentFilter = ViewBag.CurrentFilter })
</th>
<th style="width:200px">
@Html.ActionLink("Series Title", "Index", new { sortOrder = ViewBag.SerTitleSortParam, currentFilter = ViewBag.CurrentFilter })
</th>
<th style="width:200px">
@Html.ActionLink("Subject", "Index", new { sortOrder = ViewBag.SubjectSortParam, currentFilter = ViewBag.CurrentFilter })
</th>
<th style="width:200px">
@Html.ActionLink("Class Subject", "Index", new { sortOrder = ViewBag.ClassSubjSortParam, currentFilter = ViewBag.CurrentFilter })
</th>
<th style="width:300px">
@Html.ActionLink("Class Geography", "Index", new { sortOrder = ViewBag.ClassGeogSortParam, currentFilter = ViewBag.CurrentFilter })
</th>
<th style="width:300px">
@Html.ActionLink("Class Language", "Index", new { sortOrder = ViewBag.ClassLangSortParam, currentFilter = ViewBag.CurrentFilter })
</th>
<th style="width:200px">
@Html.ActionLink("Class Date", "Index", new { sortOrder = ViewBag.ClassDateSortParam, currentFilter = ViewBag.CurrentFilter })
</th>
<th style="width:250px">
@Html.ActionLink("Related Names 1", "Index", new { sortOrder = ViewBag.RelName1SortParam, currentFilter = ViewBag.CurrentFilter })
</th>
<th style="width:250px">
@Html.ActionLink("Related Names 2", "Index", new { sortOrder = ViewBag.RelName2SortParam, currentFilter = ViewBag.CurrentFilter })
</th>
<th style="width:250px">
@Html.ActionLink("Related Names 3", "Index", new { sortOrder = ViewBag.RelName3SortParam, currentFilter = ViewBag.CurrentFilter })
</th>
<th style="width:250px">
@Html.ActionLink("Related Names 4", "Index", new { sortOrder = ViewBag.RelName4SortParam, currentFilter = ViewBag.CurrentFilter })
</th>
<th style="width:250px">
@Html.ActionLink("Related Titles 1", "Index", new { sortOrder = ViewBag.RelTit1SortParam, currentFilter = ViewBag.CurrentFilter })
</th>
<th style="width:250px">
@Html.ActionLink("Related Titles 2", "Index", new { sortOrder = ViewBag.RelTit2SortParam, currentFilter = ViewBag.CurrentFilter })
</th>
<th style="width:250px">
@Html.ActionLink("Related Titles 3", "Index", new { sortOrder = ViewBag.RelTit3SortParam, currentFilter = ViewBag.CurrentFilter })
</th>
<th style="width:150px"></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.CatalogID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Product.ProductID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Product2.ProductName)
</td>
<td>
@Html.DisplayFor(modelItem => item.UnitPrice)
</td>
<td>
@Html.DisplayFor(modelItem => item.Format)
</td>
<td>
@Html.DisplayFor(modelItem => item.Quantity)
</td>
<td>
@Html.DisplayFor(modelItem => item.Availability)
</td>
<td>
@Html.DisplayFor(modelItem => item.New)
</td>
<td>
@Html.DisplayFor(modelItem => item.Location)
</td>
<td>
@Html.DisplayFor(modelItem => item.OriginalNo)
</td>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.RelatedTitle4.RelatedTitleName)
</td>
<td>
<a href="@Html.DisplayFor(modelItem => item.Link)" target="_blank">@Html.DisplayFor(modelItem => item.Link)</a>
</td>
<td>
@Html.DisplayFor(modelItem => item.Notes)
</td>
<td>
@Html.DisplayFor(modelItem => item.Notes2)
</td>
<td>
@Html.DisplayFor(modelItem => item.Type)
</td>
<td>
@Html.DisplayFor(modelItem => item.Series.SeriesName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Subject)
</td>
<td>
@Html.DisplayFor(modelItem => item.BicFull.BicFullName)
</td>
<td>
@Html.DisplayFor(modelItem => item.BicFull2.BicFullName)
</td>
<td>
@Html.DisplayFor(modelItem => item.BicFull3.BicFullName)
</td>
<td>
@Html.DisplayFor(modelItem => item.BicFull4.BicFullName)
</td>
<td>
@Html.DisplayFor(modelItem => item.RelatedName.RelatedNameName)
</td>
<td>
@Html.DisplayFor(modelItem => item.RelatedName2.RelatedNameName)
</td>
<td>
@Html.DisplayFor(modelItem => item.RelatedName3.RelatedNameName)
</td>
<td>
@Html.DisplayFor(modelItem => item.RelatedName4.RelatedNameName)
</td>
<td>
@Html.DisplayFor(modelItem => item.RelatedTitle.RelatedTitleName)
</td>
<td>
@Html.DisplayFor(modelItem => item.RelatedTitle2.RelatedTitleName)
</td>
<td>
@Html.DisplayFor(modelItem => item.RelatedTitle3.RelatedTitleName)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.CatalogID }) |
@Html.ActionLink("Details", "Details", new { id = item.CatalogID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.CatalogID })
</td>
</tr>
}
</table>
</div>
<br />
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("Index", new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))
答案 0 :(得分:0)
首先检查数据库中的关系。如果存在关系,则在删除所有内容后更新.edmx文件。我认为它会起作用。
补充提示: - 1.清洁解决方案 2.重建解决方案
答案 1 :(得分:0)
你还没有提到你正在使用什么方法,(DB First或Code First) 如果DB首先只更新您的.edmx文件并重建您的Project,否则使用迁移和更新数据库。它应该从数据库中获取该表。