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

本文关键字:模型 无效 EF Barebones MVC | 更新日期: 2023-09-27 18:17:33

在混合webforms和MVC的项目中战斗后,我现在已经开始了一个空项目,仍然看到"无效的列名'模型'。错误。裸机测试项目如下:

VS2012 ->新项目-> ASP。. NET MVC 5空项目(Visual c#),命名为testf

My Data Context 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; }
    }
}

控制器的索引方法homeconcontroller .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>

最后是数据库中表的结构

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

当控制器执行return View(db.Templates.ToList());时抛出异常-

"无效的列名'Model'.

描述:当前web请求执行过程中出现未处理的异常。请查看堆栈跟踪以获得有关错误及其在代码中的起源位置的更多信息。

异常详细信息:System.Data.SqlClient.SqlException:无效的列名'Model'。"

我可以采取生成的SQL代码(从VS异常处理程序内抓取),并运行在SQL管理器工作室的DB,它工作得很好。

我不明白这个错误,因为它的行为就像我在模型中指定了数据库表中不存在的列,或者在模型中不存在的表中有一些东西。我已经尝试过手工创建和逆向工程代码-第一和相同的错误。

你知道为什么它会在不存在的列上抛出错误吗?

我做了一个生成脚本来将表脚本到新的查询窗口,并创建了一个名为testf的新DB,并运行创建脚本来生成有问题的表。这个新的数据库工作得很好,没有错误。这个并没有解释为什么它不能在现有的db/表上工作-即使运行上述脚本在db中删除/创建不起作用的表。因此,似乎是SQL出错了,而不是代码。

更新2

有几个项目偏离了轨道

  • 最初的DB创建者没有定义任何主键,所以它没有很好地发挥和EF有一个困难的时候,我猜由于这一点。
  • 使用Code First工具没有生成正确的连接字符串(我没有改变任何东西,但我现在得到这个正确)H/T @Uber Bot为此。

定义适当的键和重新运行代码首先现有的db EF工具,我有适当的模型和连接字符串,它的工作,无论是在新的解决方案和现有的解决方案,它最初不工作。

谢谢你的见解和建议。

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

为了向生成的模型添加额外的字段,您应该创建一个局部类,例如:

你有这个生成的类:

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

感谢大家的回复。似乎在原始DB中的表中存在问题。我做了一个创建到->脚本,并在一个干净的数据库上运行它,解决方案没有问题。所以要在现有的DB中删除/重新创建表,然后重建并从那里开始。

如果我在DB表中找到罪魁祸首,我将发布调查结果,以防其他人有同样的问题。