在混合webforms和MVC的項目中對抗此項後,我現在開始了一個空白項目,但仍然看到「無效列名稱」模型錯誤。如下:MVC 5,EF 6 Barebones無效列名稱'Model'
VS2012 - >新建項目 - > ASP.NET MVC 5空項目(Visual C#中),將其命名爲testef
我的數據上下文NosContext.cs
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace testef.Domain_Models
{
public class NosContext : DbContext
{
public NosContext() : base("Name=NosContext")
{
}
public DbSet<Nos_Templates> Templates { get; set; }
}
}
我只有型號,個Nos_Templates.cs
using System;
using System.ComponentModel.DataAnnotations;
namespace testef.Domain_Models
{
public class Nos_Templates
{
[Key]
public Guid id { get; set; }
public string template_name { get; set; }
public string edit_region { get; set; }
public string question_list { get; set; }
public DateTime? create_date { get; set; }
public Guid? creating_user { get; set; }
public DateTime? update_date { get; set; }
public Guid? updating_user { get; set; }
}
}
控制器的索引方法HomeController.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using testef.Domain_Models;
namespace testef.Controllers
{
public class HomeController : Controller
{
private NosContext db = new NosContext();
// GET: /Home/
public ActionResult Index()
{
return View(db.Templates.ToList());
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
而在的web.config
<connectionStrings>
<add name="NosContext" connectionString="Data Source=localhost;Initial Catalog=testdb;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
連接字符串和的最後的結構DB中的表格
id uniqueidentifier NotNull
template_name varchar(150) Nullable
edit_region ntext Nullable
question_list ntext Nullable
create_date datetime Nullable
creating_user uniqueidentifier Nullable
update_date datetime Nullable
updating_user unique identifier Nullable
當控制器執行時返回View(db.Templates.ToList());它拋出異常 -
「無效列名‘模範’
說明:當前Web請求的執行過程中發生未處理的異常,請檢查堆棧跟蹤有關該錯誤以及更多的信息。它起源於代碼
異常詳細信息:System.Data.SqlClient.SqlException:無效的列名稱'Model'。「
我可以將生成的SQL代碼(從VS異常處理程序中抓取)並在Sql manager studio中針對數據庫運行,並且它工作得很好。
我不明白這個錯誤,因爲它的行爲就像我在模型中指定了一個不存在於數據庫表中的列或模型中不存在的東西。我曾嘗試通過手工創建代碼,並通過反向工程代碼創建,並且無論如何都是相同的錯誤
任何想法爲什麼它會在這樣的不存在的列上拋出錯誤?
UPDATE
我做了一個生成腳本,腳本表到新的查詢窗口,並創建一個新的數據庫名爲testEF就跑創建腳本生成問題的表。這個新的數據庫工作得很好,沒有錯誤。這不解釋了爲什麼它不能在現有的數據庫/表 - 即使運行所述腳本放置/創建不工作的數據庫中的表。所以它看起來在SQL中有些不妥,而不是代碼。
更新2
有幾個項目出歪斜
- 原來的DB的創造者並沒有定義任何主鍵,所以也沒玩好和EF的日子不好過我想是因爲這個。
- 使用代碼第一個工具沒有生成正確的連接字符串(我沒有改變任何東西,但我現在正確地得到這個)H/T到@Uber機器人爲此。
在定義了合適的鍵並重新運行代碼之後,我已經有了適當的模型和連接字符串,並且它在新解決方案和現有解決方案中都起作用。
感謝您的所有見解和提示。
嘗試運行SQL Server Profiler以查看EF生成的確切SQL語句。 –
SELECT [Extent1]。[ID] AS [ID], [Extent1] [TEMPLATE_NAME] AS [TEMPLATE_NAME], [Extent1] [edit_region] AS [edit_region], [Extent1] [question_list] AS [question_list], [Extent1]。[create_date] AS [create_date], [Extent1]。[creating_user] AS [creating_user], [Extent1]。[update_date] AS [update_date], [Extent1]。[updating_user ] AS [updating_user] FROM [dbo]。[Nos_Templates] AS [Extent1] 正如您所看到的,字段Model不存在或任何地方,但它一直試圖使用它。 – SouthPlatte
@SouthPlatte你使用的是MSSQL還是MySQL? – silkfire