2015-10-12 32 views
1

在混合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機器人爲此。

在定義了合適的鍵並重新運行代碼之後,我已經有了適當的模型和連接字符串,並且它在新解決方案和現有解決方案中都起作用。

感謝您的所有見解和提示。

+0

嘗試運行SQL Server Profiler以查看EF生成的確切SQL語句。 –

+0

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

+0

@SouthPlatte你使用的是MSSQL還是MySQL? – silkfire

回答

1

爲了增加額外的字段所生成的模型,您應該創建例如局部類:

你有這樣生成的類:

public partial 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; } 
    } 

要添加其他字段,你應該這樣做:

public partial class Nos_Templates 
    { 
     public string testField { get; set; } 
    } 
+0

我不添加額外的字段,並使用partial關鍵字不做任何更改。 EF聲稱我試圖選擇名爲'模型'的字段,這顯然不是我的代碼中的任何地方,我的表定義或任何地方。這就是我不明白的地方是EF從哪裏獲取鬼域參考。 – SouthPlatte

+0

從連接字符串來看,它似乎是Code-First。 –

+0

在我將其引入此測試項目之前,它首先在主項目中作爲代碼開始。我做了第一個反編譯現有數據庫的代碼。目前沒有任何關係可以定義爲我最終想要與其合作的其他三個表中的任何一個。 – SouthPlatte

1

感謝所有的答覆。看起來在原始數據庫的表格中存在問題。我做了一個創建 - >腳本,並在乾淨的數據庫上運行它,並且該解決方案沒有問題。因此,要刪除/重新創建現有數據庫中的表格,然後重建並從那裏開始。

如果我發現數據庫表中的罪魁禍首,我會公佈結果,以防其他人有相同的問題。

+0

有點兒告訴過你... – silkfire