当我最初运行我的代码时,它返回了一个无效的对象名称&#39; dbo.SalesHeaders&#39;错误。应该一直在寻找的表是dbo.SalesHeader。我能够通过this post找到this link第三篇文章中的解决方案,其中添加了modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
我的问题是为什么这首先发生?如何在不添加此行代码的情况下解决此问题?据我所知,我的其他模型和控制器的设置方式相同,并且不会在表格中添加复数。
上下文
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using Infotech.Coverpools.Portal.Tintaglia.CodeFirst.Models.Mapping;
using System.Data.Entity.ModelConfiguration.Conventions;
namespace Infotech.Coverpools.Portal.Tintaglia.CodeFirst.Models
{
public partial class TintagliaContext : DbContext
{
static TintagliaContext()
{
Database.SetInitializer<TintagliaContext>(null);
}
public TintagliaContext()
: base("Name=TintagliaContext")
{
}
public DbSet<Configuration> Configurations { get; set; }
public DbSet<Customer> Customers { get; set; }
//New for Account Module
public DbSet<UserProfile> UserProfiles { get; set; }
public DbSet<Role> Roles { get; set; }
public DbSet<webpages_UsersInRoles> webpages_UsersInRole { get; set; }
// public DbSet<Customer> Customers { get; set; }
public virtual DbSet<CustLedgerEntry> CustLedgerEntries { get; set; }
public virtual DbSet<SalesHeader> SalesHeaders { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new ConfigurationMap());
modelBuilder.Configurations.Add(new CustomerMap());
modelBuilder.Configurations.Add(new CustLedgerMap());
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); //line that fixed this//
}
}
}
模型
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
namespace Infotech.Coverpools.Portal.Tintaglia.CodeFirst.Models
{
public class SalesHeader
{
public string CustomerNo_ { get; set; }
public DateTime OrderDate { get; set; }
[Key]
public string No_ { get; set; }
public string DocumentType { get; set; }
public string ExternalDocumentNo_ { get; set; }
public DateTime ShipmentDate { get; set; }
public string ShippingAgentCode { get; set; }
public DateTime DatetoWebSales { get; set; }
}
}
控制器
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Infotech.Coverpools.Portal.Tintaglia.CodeFirst.Models;
namespace Infotech.Coverpools.Portal.Tintaglia.Web.Controllers
{
public class SalesHeaderController : Controller
{
private TintagliaContext db = new TintagliaContext();
//
// GET: /SalesHeader/
public ActionResult Index()
{
IEnumerable<SalesHeader> ListSalesheaders;
//using (TintagliaContext db = new TintagliaContext())
{
ListSalesheaders = db.SalesHeaders.ToList();
}
return View(ListSalesheaders);
//return View(db.SalesHeaders.ToList());
}
查看
@model IEnumerable<Infotech.Coverpools.Portal.Tintaglia.CodeFirst.Models.SalesHeader>
@using GridMvc.Html
@{
ViewBag.Title = "Open Orders";
}
<h2>Open Orders</h2>
<h2>Open Orders</h2>
<link href="~/Content/Gridmvc.css" rel="stylesheet" />
<link href="~/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.9.1.min.js"></script>
<script src="~/Scripts/gridmvc.js"></script>
@Html.Grid(Model).Columns(columns =>
{
columns.Add(m => m.CustomerNo_).Titled("Account Number").Filterable(true);
columns.Add(m => m.OrderDate).Titled("Order Date").Filterable(true);
columns.Add(m => m.No_).Titled("Order Number").Filterable(false);
}).WithPaging(10).Sortable(true)