我正在编写单元测试用例,并且我成功地为 编写了单元测试用例Query。但我无法为 编写单元测试用例QueryMultiple。
对于查询我是这样写的:
IEnumerable<ClientTestPurpose> fakeTestPurposes = new
List<ClientTestPurpose>()
{
new ClientTestPurpose { PurposeID = 1, PurposeName = "Test Purpose name1"},
new ClientTestPurpose { PurposeID = 1, PurposeName = "Test Purpose name2"},
new ClientTestPurpose { PurposeID = 1, PurposeName = "Test Purpose name3"}
};
_mock.SetupDapper(x => x.Query<ClientTestPurpose>(It.IsAny<string>(), null, null, true, null, null)).Returns(fakeTestPurposes);
var result = _libraryRepository.TestPurposes(clientModal.Id);
Assert.IsNotNull(result);
Assert.AreEqual(result.Count(), fakeTestPurposes.Count());
Run Code Online (Sandbox Code Playgroud)
如何写QueryMultiple:
using (var multi = _db.QueryMultiple(spName, spParams, commandType: CommandType.StoredProcedure))
{
var totals = multi.Read<dynamic>().FirstOrDefault();
var …Run Code Online (Sandbox Code Playgroud) 当给定值可用时,以下 SQL 查询返回 1;当给定值不可用时,以下 SQL 查询返回 0,并且它在 SQL Server Management Studio 中工作正常,
select
case
when exists (select * from [dbo].[user]
where userName = 'admin'
and password = 'admin')
then cast(1 as bit)
else cast(0 as bit)
end
Run Code Online (Sandbox Code Playgroud)
使用 dapper ORM 的相同查询如下:
public int login(string userName, string password)
{
string sql = "select case when exists(select * from user where userName = @usernamepara and password = @passwordpara) then cast(1 as bit) else cast(0 as bit )end";
using(IDbConnection conn = dbConnection)
{ …Run Code Online (Sandbox Code Playgroud) 使用 Dapper 从 .NET Entity Framework 调用存储过程时出现此错误。
不允许从数据类型 sql_variant 隐式转换为 varchar。
存储过程参数为@Name nvarchar(50) = NULL。
因此理想情况下,它应该接受空值。当我调用存储过程并@Name = null从 .NET 传递时,出现此错误。
这是存储过程:
CREATE PROCEDURE [dbo].[Member_Add]
@Id NVARCHAR(50) = NULL,
@Name NVARCHAR(50) = NULL
AS
BEGIN
INSERT INTO [dbo].Member (Id, Name)
VALUES (@Id, @Name)
END
Run Code Online (Sandbox Code Playgroud) 我目前在一层中有两个类,它们执行将数据包含在数据库中的操作:
using Dapper;
using System;
using System.Data.SqlClient;
using System.Linq;
namespace repositories
{
public class DAOBook
{
private readonly string _connection;
public DAOBook(string databaseConnection)
{
_connection = databaseConnection;
}
public bool IncludeBook(string title)
{
try
{
using (var connection = new SqlConnection(_connection))
{
var sql = $@"
INSERT INTO books
(title)
VALUES
('{title}' ";
var result = connection.Execute(sql);
return result != 0;
}
}
catch (Exception ex)
{
throw new Exception($"{ex.Message}", ex);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
using Dapper;
using System;
using …Run Code Online (Sandbox Code Playgroud) 我必须调用参数为char和 的存储过程VarBinary(MAX)。我需要使用 Dapper 从 C# 代码调用此存储过程。但我找不到 dapper 支持的任何参数。
SP:
ALTER PROCEDURE [dbo].[uspTest]
, @Param1 CHAR(1)
, @Param2 VARBINARY(MAX)=null
Run Code Online (Sandbox Code Playgroud)
C#:
DynamicParameters parameter = new DynamicParameters();
parameter.Add("@Param1", email.SenderType, DbType.char, ParameterDirection.Input);
parameter.Add("@Param2", email.AttachedFileStream, DbType.varbinary, ParameterDirection.Input);
Run Code Online (Sandbox Code Playgroud)
编译错误:
DBType 不包含 char 和 varbinary 的定义
我有一个模型:
public class Model
{
public int Id { get;}
public string Name { get; }
public string AnotherName { get; }
}
Run Code Online (Sandbox Code Playgroud)
默认构造函数,没有setter,因此我们在IL生成的类中没有公共setter方法。
但Dapper以某种方式初始化我的数据。所有属性均已填充。
var sql = $@"SELECT id as Id, name as Name, another_name as AnotherName FROM dapper";
var raws = (connection.QueryAsync<Model>(sql).Result).AsList();
Run Code Online (Sandbox Code Playgroud)
我找到了源代码,并且它们通过 Setter 方法进行设置,但是当我尝试像这样获取 setter 时Dapper,我得到了 null methodInfo。这是一些DapperSqlMapper的源代码:3320
if (specializedConstructor == null)
{
// Store the value in the property/field
if (item.Property != null)
{
il.Emit(type.IsValueType ? OpCodes.Call : OpCodes.Callvirt, DefaultTypeMap.GetPropertySetter(item.Property, …Run Code Online (Sandbox Code Playgroud) 我使用vb.net和微型ORM DAPPER.NET启动了一个新项目。通过nuGet,我集成了dapper和dapper.simpleCRUD。我要执行某些CRUD操作时出现问题,我对SqlMapper.cs有问题。我不知道为什么,因为我已成功将“ dapper.simpleCRUD”扩展集成到项目中。怎么了?




我尝试手动添加SqlMapper.cs文件,我得到了:
我还没有开始使用Dapper,但是当我正在研究批量插入/更新时,我今天偶然发现了它.目前我正在使用EF6,但我希望将来使用Dapper我的大量内容.对于这个应用程序,可能有~15k记录,但我有其他应用程序,可能是~100k记录.
我正在尝试研究一种可以将以下EF代码转换为Dapper的方法.它只是从文件中读取记录,查看该员工是否存在于DB中,如果是,则使用文件中的值更新属性,如果不是,则使用文件中的值创建新对象.
当我环顾四周时,我找不到任何例子.我能找到的只是如何进行简单的插入或更新.我没有找到批量插入/更新的好例子.我很可能还没有理解如何使用Dapper.
我怎么用Dapper这样做?
int count = 1;
using (ctx = new DataContext())
{
ctx.Configuration.AutoDetectChangesEnabled = false;
ctx.Configuration.ValidateOnSaveEnabled = false;
while ((record = srFile.ReadLine()) != null)
{
int employeeId = int.Parse(record.Substring(2, 8));
bio_employee employee = ctx.bio_employee.FirstOrDefault(e => e.emp_id == employeeId);
if (employee != null)
{
SetEmployeeData(employee, record);
ctx.Entry(employee).State = System.Data.Entity.EntityState.Modified;
}
else
{
employee = new bio_employee();
employee.emp_id = employeeId;
SetEmployeeData(employee, record);
ctx.bio_employee.Add(employee);
}
if (count % batchSize == 0)
{
ctx.SaveChanges();
ctx.Dispose();
ctx = new DataContext();
}
count++;
} …Run Code Online (Sandbox Code Playgroud) 我Location在User桌面上配置了EF 字段:
public DbGeography Location { get; set; }
Run Code Online (Sandbox Code Playgroud)
但是当我查询我的User表时:
user = connection.Query<User>("update [User] set LastOnline = @lastOnline output INSERTED.* where Username = @un",
new { lastOnline = DateTime.UtcNow, un = username }).First();
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
消息=解析列122时出错(Location = POINT(-118.2436849 34.0522342) - Object)Source = Dapper StackTrace:在d:\ Dev\dapper-中的Dapper.SqlMapper.ThrowDataException(Exception ex,Int32 index,IDataReader reader,Object value) dot-net\Dapper NET40\SqlMapper.cs:Dapper.SqlMapper.d__11中的Deserialize4650b5f0-d037-49ad-802e-8a9be95e8496(IDataReader)的4045行(IDataReader)Dapper.SqlMapper.Query [T]中的
1.MoveNext() in d:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 1572 at System.Collections.Generic.List1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) (IDbConnection cnn,String sql,Object param,IDbTransaction transaction,Boolean buffered,Nullable1 commandTimeout, Nullable …
我有一个针对.net 4.6和Dapper 1.50.4的WPF/MVVM Light应用程序我使用Visual Studio Pro 2017来开发应用程序
这个应用程序正在运行,但我有一台新计算机并将此应用程序的开发移至新计算机.当我尝试在我的新计算机上运行此应用程序时,我收到以下错误:
System.IO.FileLoadException: '无法加载文件或程序集'Dapper,Version = 1.50.4.0,Culture = neutral,PublicKeyToken = null'或其依赖项之一.需要一个强名称的程序集.(来自HRESULT的异常:0x80131044)'
Run Code Online (Sandbox Code Playgroud)The operation failed. Bind result: hr = 0x80070002. The system cannot find the file specified. Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll Running under executable C:\Users\jorda\...\bin\Debug\FTC_Application.exe --- A detailed error log follows. === Pre-bind state information === LOG: DisplayName = System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e (Fully-specified) LOG: Appbase = file:///C:/Users/jorda/.../bin/Debug/ LOG: Initial PrivatePath = NULL LOG: Dynamic Base = NULL LOG: Cache …
dapper ×10
c# ×5
.net ×3
sql-server ×3
vb.net ×2
asp.net-core ×1
linq ×1
moq ×1
mvvm-light ×1
oracle ×1
orm ×1
reflection ×1
transactions ×1
unit-testing ×1
wpf ×1