【问题标题】:MVC error- An exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.SqlServer.dll but was not handled in user codeMVC 错误 - EntityFramework.SqlServer.dll 中出现“System.Data.SqlClient.SqlException”类型的异常,但未在用户代码中处理
【发布时间】:2016-05-09 12:42:51
【问题描述】:

我是 MVC(和 asp.net)的新手。当我执行代码时出现此错误消息:

 An exception of type 'System.Data.SqlClient.SqlException' occurred in   
 EntityFramework.SqlServer.dll but was not handled in user code
 Additional information: Invalid object name 'delays_table'.

这是我的控制器:

namespace public_transportaion.Controllers
{
public class StatisticsController : Controller
{
    private StatisticDBContext db = new StatisticDBContext();

    // GET: Statistics
    public ActionResult Index()
    {
        string query = "SELECT agency_name, COUNT(*) "
   + "FROM delays_table "
   + "WHERE late_type = 'miss' "
   + "GROUP BY agency_name";

        IEnumerable<Statistic> data = db.Database.SqlQuery<Statistic>(query);

        return View(data.ToList());

    }

这是我的模特:

namespace public_transportaion.Models
{
public class Statistic
{
    public int ID { get; set; }
    public string agency_name { get; set; }
    public int missCount { get; set; }
}

public class StatisticDBContext : DbContext
{
    public DbSet<Statistic> Statistics { get; set; }
}
}

这里是数据库信息:

CREATE TABLE [dbo].[delays_table] 
[ID]                     INT            IDENTITY (1, 1) NOT NULL,
[tripID]                 BIGINT         NOT NULL,
[planned_arrival_timeID] DATETIME       NOT NULL,
[route_id]               BIGINT         NOT NULL,
[agency_name]            NVARCHAR (MAX) NULL,
[startDate]              DATETIME       NOT NULL,
[endDate]                DATETIME       NOT NULL,
[late_type]              NVARCHAR (MAX) NULL,
[duration]               REAL           NOT NULL,
CONSTRAINT [PK_dbo.delays_table] PRIMARY KEY CLUSTERED ([ID] ASC)

我也尝试在查询中使用“FROM dbo.delays_table”或“FROM delays_table (dbo)”,但没有帮助。

谢谢。

【问题讨论】:

  • 您的连接字符串是否指向服务器上正确的数据库?
  • 在 try catch 块中编写代码
  • 您可能想检查您的连接字符串指向的位置

标签: c# asp.net-mvc


【解决方案1】:

你还需要定义delays_table

类似这样的:

namespace public_transportaion.Models //or ORM
{
    [Table("delays_table", Schema = "YourSchemaHere")]
    public class delays_table
    {
        [Key]
        public string agency_name { get; set; },
        public int COUNT { get; set; }
    }
}

【讨论】:

    【解决方案2】:

    听起来这里可能会发生以下两种情况之一:您的连接字符串指向错误的数据库或您的 SQL 查询无法识别 delays_table 实际上是一个表:

    检查您的连接字符串

    您的数据上下文的连接字符串指向的数据库与您预期的不同(考虑检查您的 web.config 文件以确保它正确):

    <connectionStrings>
        <add name="DefaultConnection" connectionString="{check-me}" providerName="System.Data.SqlClient" />
    </connectionStrings>
    

    您将主要检查您的服务器和数据库名称是否正确:

    Data Source={your-database-server};Initial Catalog={your-database-name};
    

    明确表明您正在使用表格

    您可以尝试将您的表名显式括在方括号中,让 SQL 知道您的目标是一个表:

    // The [delays_table] will explicitly indicate you are targeting a table with that name
    var query = "SELECT agency_name, COUNT(*) FROM [delays_table] WHERE late_type = 'miss' GROUP BY agency_name";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多