我刚刚开始使用Dapper进行一个项目,过去几年大多使用像NHibernate和EF这样的ORM.
通常在我们的Web应用程序中,我们按请求实现会话,在请求开始时开始事务并在结束时提交它.
我们应该在直接使用SqlConnection/System.Transactions时做类似的事情吗?
StackOverflow如何做到这一点?
根据@gbn和@Sam Safron的建议,我没有使用交易.在我的情况下,我只是在进行读取查询,因此似乎没有真正的要求使用事务(与我所知道的隐式事务相反).
我创建了一个轻量级的会话接口,以便每个请求都可以使用一个连接.这对我来说非常有益,因为对于Dapper我经常需要创建一些不同的查询来构建一个对象,而宁愿共享相同的连接.
确定每个请求的连接并处理它的工作是由我的IoC容器(StructureMap)完成的:
public interface ISession : IDisposable {
IDbConnection Connection { get; }
}
public class DbSession : ISession {
private static readonly object @lock = new object();
private readonly ILogger logger;
private readonly string connectionString;
private IDbConnection cn;
public DbSession(string connectionString, ILogger logger) {
this.connectionString = connectionString;
this.logger = logger;
}
public IDbConnection Connection { get { return GetConnection(); } }
private IDbConnection GetConnection() {
if (cn == null) {
lock (@lock) …Run Code Online (Sandbox Code Playgroud) 我是评价精致但我已经遇到了一些问题.
我想这样做
using (IDbConnection connection = GetConnection())
{
connection.Open();
var result = connection.Query(
"select * from myTable where ID_PK = @a;", new { a = 1 });
}
Run Code Online (Sandbox Code Playgroud)
它在SqlMapper.cs的第393行抛出ORA-00936:缺少表达式OracleException
using (var reader = cmd.ExecuteReader())
Run Code Online (Sandbox Code Playgroud)
当我删除参数时,我将整个表放入结果变量中.
查询在sqldeveloper中没有问题.我正在使用Oracle.DataAccess Assembly 2.112.2.0
我目前正在构建一个将12个表连接在一起的SELECT查询.我一直在使用Dapper进行所有其他查询,效果很好.问题是,泛型方法只需要五个通用参数.
我之前修改过代码以支持最多6个用于另一个查询,但现在我真的不认为我应该破解6个更多级别的泛型.
有没有办法将dapper传递给一个类型数组,并将结果作为一个对象数组返回,如果必须的话我可以手动编译?
我也可能以错误的方式接近问题!任何帮助将不胜感激!
我一直在使用Dapper和我目前的项目,我将不得不使用ADO.NET.我的问题是如何使用ADO.NET返回IEnumerable?这是我使用Dapper的原因.有人可以帮我转换这个但是用ADO做同样的事吗?
public IEnumerable<Favorites> GetFavorites()
{
using (SqlConnection sqlConnection = new SqlConnection(connString))
{
sqlConnection.Open();
var work = sqlConnection.Query<Favorites>("Select * from favorites");
return work;
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用dapper与Oracle(ODP.NET),我想使用"QueryMultiple"功能.
将此字符串传递给QueryMultiple方法:
var query = "Select CUST_ID CustId from Customer_info WHERE CUST_ID=:custId;" +
"Select CUST_ID CustId from BCR WHERE CUST_ID=:custId";
Run Code Online (Sandbox Code Playgroud)
我收到一个ORA-00911:无效的字符错误
有没有办法做到这一点或不可能?
TKS
我正在使用Dapper(感谢Sam,很棒的项目.)带有DAL的微型ORM,由于某种原因,我无法使用输入参数执行存储过程.
在示例服务中,我有以下代码:
public void GetSomething(int somethingId)
{
IRepository<Something, SomethingEnum> repository = UnitOfWork.GetRepository<Something, SomethingEnum>();
var param = new DynamicParameters();
param.Add("@somethingId", dbType: DbType.Int32, value:somethingId, direction: ParameterDirection.Input);
var result = repository.Exec<Something>(SomethingEnum.spMyStoredProcedure, param);
...
}
Run Code Online (Sandbox Code Playgroud)
当触发执行存储过程时,抛出SqlException声明我需要提供'somethingId'
过程或函数'spMyStoredProcedure'需要参数'@somethingId',这是未提供的.
我的DAL类似于Pencroff的这个github项目.
我在这里错过了什么吗?
更新:我实际上是通过SomethingEnum传递commandType:
public class SomethingEnum : EnumBase<SomethingEnum, string>
{
public static readonly SomethingEnum spMyStoredProcedure = new SomethingEnum("spMyStoredProcedure", "[dbo].[spMyStoredProcedure]", CommandType.StoredProcedure);
public SomethingEnum(string Name, string EnumValue, CommandType? cmdType): base(Name, EnumValue, cmdType)
{
}
}
Run Code Online (Sandbox Code Playgroud) 所以我正在尝试使用Dapper.net,我很喜欢它.我不喜欢的是当我尝试批量插入实体时,我得到以下错误抛出:
DynamicMethod的类型所有者无效.
在System.Reflection.Eynamic.DynamicMethod.Init(String name,MethodAttributes属性,CallingConventions callingConvention,Type returnType,Type [] signature,Type owner,Module m,Boolean skipVisibility,Boolean transparentMethod,StackCrawlMark&stackMark)at System.Reflection.Emit. Dapper.SqlMapper.CreateParamInfoGenerator中的DynamicMethod..ctor(String name,Type returnType,Type [] parameterTypes,Type owner,Boolean skipVisibility)
1 literals) in D:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 3033 at Dapper.SqlMapper.GetCacheInfo(Identity identity, Object exampleParameters, Boolean addToCache) in D:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 2138 at Dapper.SqlMapper.<QueryImpl>d__61(D:\ Dev \中的身份标识,布尔checkForDuplicates,Boolean removeUnused,IList 1.MoveNext()) System.Linq.Enumerable.ToList [TSource]中的dapper-dot-net\Dapper NET40\SqlMapper.cs:System.Collections.Generic.List1..ctor(IEnumerable1 collection的第1578行)
(IEnumerable1 source) at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable1 commandTimeout,Nullable1 commandType) in D:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 1479 at Dapper.SqlMapper.Query(IDbConnection cnn, String sql, Object param, …
我知道有几个类似于我的问题.
但我不认为上述两个问题都有明确的答案符合我的要求.
现在我开发了一个新的WebAPI项目,并在WebAPI项目和DataAccess技术之间进行了划分.我没有问题测试Controller for WebAPI因为我可以模拟数据访问类.
但对于DataAccess类来说,这是一个不同的故事,因为我在其中使用Dapper内联查询,我有点困惑,我如何通过使用单元测试来测试它.我问了一些朋友,他们更喜欢做集成测试而不是单元测试.
我想知道的是,是否可以对使用Dapper和Inline查询的DataAccess类进行单元测试.
假设我有一个这样的类(这是一个通用的存储库类,因为很多代码都有类似的查询按表名和字段区分)
public abstract class Repository<T> : SyncTwoWayXI, IRepository<T> where T : IDatabaseTable
{
public virtual IResult<T> GetItem(String accountName, long id)
{
if (id <= 0) return null;
SqlBuilder builder = new SqlBuilder();
var query = builder.AddTemplate("SELECT /**select**/ /**from**/ /**where**/");
builder.Select(string.Join(",", typeof(T).GetProperties().Where(p => p.CustomAttributes.All(a => a.AttributeType != typeof(SqlMapperExtensions.DapperIgnore))).Select(p => p.Name)));
builder.From(typeof(T).Name);
builder.Where("id = @id", new { id });
builder.Where("accountID = @accountID", new { accountID = accountName });
builder.Where("state != 'DELETED'");
var result …Run Code Online (Sandbox Code Playgroud) 使用Dapper ORM检查记录是否存在的最简单方法是什么?
我是否真的需要为查询定义POCO对象,我只想检查记录是否存在?