MVC 5,EF 6 Barebones列名无效'模型'

时间:2015-10-12 21:46:07

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

在混合webforms和MVC的项目中进行斗争之后,我现在开始了一个空项目,仍然看到"无效的列名称'模型'。错误。准系统测试项目如下:

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()); 时,它会抛出异常 -

&#34;列名称无效&#39;型号&#39;。

描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.Data.SqlClient.SqlException:列名称无效&#39;模型&#39;。&#34;

我可以使用生成的SQL代码(从VS异常处理程序中获取)并在Sql manager studio中对数据库运行,它运行正常。

我不理解错误,因为它的行为就像我在模型中指定了一个数据库表中不存在的列,或者表中的某些内容在模型中不存在。我尝试手工创建和逆向工程代码优先,同样的错误无论如何。

为什么它会像这样在不存在的列上抛出错误?

更新

我做了一个生成脚本,将表脚本编写到新的查询窗口,并创建了一个名为testEF的新数据库,并运行了创建脚本来生成有问题的表。这个新数据库工作正常,没有错误。这没有解释为什么它不能在现有的db / table上工作 - 即使在运行所述脚本来删除/创建不工作的DB中的表时也是如此。所以在SQL中看起来有些不对,而不是代码。

更新2

有一些偏斜的物品

  • 原始数据库创建者没有定义任何主键,所以它没有好玩,而且由于这个原因我觉得EF很难。
  • 使用Code First工具没有生成正确的连接字符串(我没有改变任何东西,但我现在正确地得到了这个)H / T给@Uber Bot这个。

在定义了正确的密钥并重新运行代码后,我首先使用了适当的模型和连接字符串,并且它在新解决方案和现有解决方案中都起作用,但初始化不起作用。

感谢所有的见解和提示。

2 个答案:

答案 0 :(得分: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; }   
    }

答案 1 :(得分:1)

感谢所有回复。看来原始数据库中的表存在问题。我做了一个创建 - &gt;脚本并在干净的数据库上运行它,解决方案可以正常运行。因此,在现有数据库中删除/重新创建表,然后重建并从那里继续。

如果我在数据库表中找到罪魁祸首,我会发布调查结果以防其他人遇到同样的问题。

相关问题