Tag: dapper

我可以将DynamicParameters与Template一起使用并在dapper中使用返回参数吗?

我目前正在处理的系统使用存储过程进行所有数据访问。 我现在正在研究Dapper(到目前为止它看起来很棒)但是我想知道我是否可以使用使用模板创建的DynamicParameters对象,但是将其中一个参数作为输出参数。 例如: SP: CREATE PROCEDURE InsertPerson @ID int Output, @Name varchar(100), @DOB DateTime2 AS –INSERT STATEMENT SET @ID = SCOPE_IDENTITY() POCO: internal class Person { public int ID { get; set; } public string Name { get; set; } public DateTime DOB { get; set; } } 码: var procParams = new DynamicParameters(person); connection.Execute(“InsertPerson”, […]

使用Linq-to-Sql表达式生成SQL子句

我想创建一个可以使用Expression并使用Linq-To-Sql生成所需SQL语句的存储库模型。 例如,我有一个这样的function: // Possible criteria Expression<Func> criteria1 = p => p.Price > 1000; // Function that should take that criteria and convert to SQL statement static IEnumerable GetCustomers (Expression<Func> criteria) { // … } 在函数内部,我想使用Linq-To-Sql将条件转换为SQL语句。 我知道你可以使用DataContext.Log来查看执行的查询和DataContext.GetCommand(query).CommandText以便在执行之前查看完整的查询。 但是,我想只生成整个表达式的一部分。 我希望完成的是使我的存储库抽象出底层技术(Linq-to-Sql,Dapper等)。 这样我就可以将Expression传递给存储库,让它生成正确的语句并使用正确的技术来执行它。

如何在Dapper中使用generics和Nullable 类型进行实现?

我有这个电话: public IObservable Subscribe(object topic, string service, string connectionString, string query) { try { this.connection.ConnectionString = connectionString; this.connection.Open(); this.connection.Query(query, new { transactionid = topic }).ToObservable().Subscribe(message => this.subject.OnNext(message)); return this.subject; } catch (Exception e) { this.subject.OnError(e); return this.subject; } finally { this.subject.OnCompleted(); this.connection.Close(); } } 这是我的查询: with IDS as (select L1_ID, L2_ID, L1_VALUE, L2_VALUE from MYDB […]

Dapper:unit testingSQL查询

我开始使用微型ORM的Dapper,我使用的是Dapper Rainbow。 我想测试查询和它们检索的数据。 我的意思是,例如,我有UserService和方法GetAll() ,我想测试sql查询是从一些列表中检索所有用户(而不是从数据库,因为我希望测试快速)。 你知道我怎么能这样做吗? 我的服务类(以及我想测试的方法): public static class UserService{ public static IEnumerable GetAll(){ return DB.Users.All(); } } 您对unit testing查询和数据检索有什么建议吗? 谢谢

使用dapper进行generics参数化sql查询

我正在使用这种通用方法: protected IEnumerable ExecuteSprocQuery(string sproc, object objectParams) { using (var conn = OpenConnection()) { var list = conn.Query(sproc, null, commandType: CommandType.StoredProcedure); return list; } } 调用这样的存储过程: ExecuteSprocQuery(“SomeSproc”, new { P1 = p1, P2 = p2 }).ToList().FirstOrDefault(); 我正在尝试为参数化的sql查询实现类似的东西: protected IEnumerable ExecuteQuery(string sqlString, object objectParams) { using (var conn = OpenConnection()) { var list = conn.Query(sqlString, null, […]

如何使用dapper将DbGeography插入SQL Server

我using System.Data.Entity.Spatial;创建了模型using System.Data.Entity.Spatial; public class Store { public int Id { get; private set; } public string Name { get; set; } public string Address { get; set; } public DbGeography Location { get; set; } } 插入数据库 using (SqlConnection conn = SqlHelper.GetOpenConnection()) { const string sql = “INSERT INTO Stores(Name, Address, Location) ” + […]

使用DapperExtensions进行谓词化

我试图用DapperExtensions创建一个通用的Find方法 这是我的方法 public IEnumerable Find(Expression<Func> expression) { using (IDbConnection cn = GetCn()) { cn.Open(); var predicate = Predicates.Field(expression, Operator.Eq, true); return cn.GetList(predicate); } } 但是我在这行上得到了System.NullReferenceException var predicate = Predicates.Field(expression, Operator.Eq, true); 这是来自DapperExtensions帮助文档但我尝试将其转换为Generic方法。 using (SqlConnection cn = new SqlConnection(_connectionString)) { cn.Open(); var predicate = Predicates.Field(f => f.Active, Operator.Eq, true); IEnumerable list = cn.GetList(predicate); cn.Close(); }

Dapper.SimpleCRUD插入/更新/获取失败并显示消息“实体必须至少有一个属性”

我是Dapper的新生儿。 尝试将CRUD操作与Dapper和Dapper.SimpleCRUD lib结合使用。 这是示例代码…… 我的数据模型看起来像 Class Product { public string prodId {get;set;} public string prodName {get;set;} public string Location {get;set;} } 精巧的实施 – 插入 public void Insert(Product item) { using(var con = GetConnection()) { con.Insert(item); } } 由于Db中的ProdId是标识列,因此失败。 它如何表明ProdId是DB中的标识列? 精巧的实施 – 获取 public IEnumerable GetAll() { IEnumerable item = null; using (var con = GetConnection()) […]

在Dapper中比较QUERY和EXECUTE

我想问一下使用DAPPER插入,更新,删除,删除,查询特定数据时最好使用什么? 我真的很困惑在DAPPER中使用EXECUTE和QUERY命令..

对于ReturnValue参数值,Dapper获取“指定的强制转换无效。”

我正在尝试使用SCOPE_IDENTITY将长主键返回到c#,使用DynamicParameter的ReturnValue选项。 以下是Dapper网站的示例代码: var p = new DynamicParameters(); p.Add(“@a”, 11); p.Add(“@b”, dbType: DbType.Int32, direction: ParameterDirection.Output); p.Add(“@c”, dbType: DbType.Int32, direction: ParameterDirection.ReturnValue); cnn.Execute(“spMagicProc”, p, commandType: commandType.StoredProcedure); int b = p.Get(“@b”); int c = p.Get(“@c”); 我宁愿做以下操作,而不是返回int,因为我的主键字段应该是bigint var p = new DynamicParameters(); p.Add(“@a”, 11); p.Add(“@b”, dbType: DbType.Int32, direction: ParameterDirection.Output); p.Add(“@c”, dbType: DbType.Int64, direction: ParameterDirection.ReturnValue); cnn.Execute(“spMagicProc”, p, commandType: commandType.StoredProcedure); int b […]