【问题标题】:Dapper Stored procedure and ViewDapper 存储过程和视图
【发布时间】:2016-05-08 08:59:45
【问题描述】:

我是 ASP.net MVC 的新手(我正在使用 asp 5 mvc 6)

所以我想使用存储过程,我发现 Dapper-dot-net 是解决方案。

我创建了存储过程返回的模型

    namespace WebCMS.Dapper
   {
    public class ArticleGetAll
    {
        public int ArticleID { get; set; }
        public string Title { get; set; }
        public string Cotent { get; set; }
        public DateTime DateCreated { get; set; }
        public string Username { get; set; }
    }
}

我为命令创建了 .cs 文件

    public class ArticleAccess
   {
    private IDbConnection db = Connection.GetConnection();

    public List<ArticleGetAll> GetAll()
    {
        string procedureName = "usp_ArticleGetAll";
        return db.Query<ArticleGetAll>(procedureName).ToList();

    }
}

我想从模型创建 VIEW,我收到了这个错误 Error 1

以及 DbContext 需要看起来如何? Dapper 需要 DbContext 吗?

我有用于打开 SQL 连接的类 Connection.cs

    namespace WebCMS.Dapper
{
    public class Connection
    {
        public static SqlConnection GetConnection()
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
            con.Open();

            return con;
        }
    }
}

我应该把 :DbContext 放在这个类中并添加 DB Sets 吗?

【问题讨论】:

  • Dapper 不需要 DbContext !为什么要将 EF 与 Dapper 混合使用?
  • 不相关,但 Connection 变量应该是您使用它的方法的本地变量,并包含在 using 语句中
  • Steve Ok thx 关于@Shyju 的建议所以我需要在没有这个生成器的情况下创建视图吗?

标签: c# asp.net-mvc dapper


【解决方案1】:

如果您查看 Dapper 文档中的 stored procedures,那么您会看到他们为此使用了参数 commandType

var user = cnn.Query<User>("spGetUser", new {Id = 1}, 
    commandType: CommandType.StoredProcedure).SingleOrDefault();

我认为这应该可以解决您的问题。错误信息很奇怪,因为DbContext与Dapper没有任何关系。

【讨论】:

  • 我认为错误消息只是 VS IDE 插件中的一个非常适合;与 dapper 无关
  • 我输入了那个命令,但我无法添加视图:/ 我尝试添加/删除 Dapper 但它不起作用 -.-
  • “添加视图”是什么意思?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-29
  • 1970-01-01
相关资源
最近更新 更多