如何使用dapper/dapper extensions/dapper rainbow或any忽略模型上的属性
那些精致的图书馆?
我正在使用 DapperExtensions v4.0.30319,我试图让 Dapper.Contrib 知道我的架构不是 DBO。我提供了:
public class EngineMapper : ClassMapper<Engine>
{
public EngineMapper() : base()
{
Schema("vehicles");
}
}
Run Code Online (Sandbox Code Playgroud)
我从 DapperExtensions 文档(https://github.com/tmsmith/Dapper-Extensions/wiki/Customized-mapping-for-a-class)中了解到,将使用反射自动找到此类?
但我也尝试明确使用:
DapperExtensions.DapperExtensions.DefaultMapper = typeof(EngineMapper);
Run Code Online (Sandbox Code Playgroud)
无论哪种方式,当我使用 Dapper.Contrib 时:
SqlConnection.Insert(new Engine());
Run Code Online (Sandbox Code Playgroud)
结果插入语句没有指定任何模式。
如何使用 Dapper.Contrib 执行插入(或更新等),其中它使用我指定的表模式?
我有一个表有一个由两列组成的主键,这两列都没有自动递增,我的Dapper插入(Dapper Extensions的一部分)在插件上失败,说两列中的第一列不允许为null ,即使我传入的值也不是空的.
表Student
:
StudentId (PK, not null) \_ (combined to form primary key)
StudentName (PK, not null) /
Active -- just another column
Run Code Online (Sandbox Code Playgroud)
C#:
public class Student {
public int StudentId { get; set; }
public string StudentName { get; set; }
public bool Active { get; set; }
}
var newStudent = new Student { StudentId = 5, StudentName = "Joe", Active = true };
var insertSuccess = myConn.Insert<Student>(newStudent);
Run Code Online (Sandbox Code Playgroud)
错误:
无法将值NULL插入列'StudentId',表'dbo.Student'; 列不允许空值.INSERT失败.
Dapper由于某种原因没有获得StudentId
值为5.我是否必须为具有组合PK的表或具有不 …
我有2个简单的表.
create table Owner
(
Id int primary key,
Name nvarchar(100),
);
create table Status
(
Id int primary key,
BrandName nvarchar(50)
OwnerId int foreign key references Owner(Id),
);
Run Code Online (Sandbox Code Playgroud)
在应用程序中,我将这些表映射到模型类:
public class Owner
{
public int Id {get;set;}
public string Name{get;set;}
public Status Status {get;set;}
}
public class Status
{
public int Id {get;set;}
public string Brand {get;set;}
public int OwnerId {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
我用dapper和dapper扩展.
我想在classmapper中的dapper中一对一地映射关系.这是可能的?
我的目标是当我添加所有者对象时,它还通过存储库设置了属性Status到db,它还插入了记录执行状态表.
实现这种行为的最佳方法是什么?
public class OwnerMapper : ClassMapper<Owner>
{
public OwnerMapper()
{
Table("Owner");
Map(p=>p.Id).Column("Id").Key(KeyType.Assigned);
Map(p=>p.Name).Column("Name");
//how …
Run Code Online (Sandbox Code Playgroud) 我有一个具有以下属性的Customer类:
public int Id { get; set; }
public string Name { get; set; }
public int AddressId { get; set; }
public Address Address { get; set; }
Run Code Online (Sandbox Code Playgroud)
我的目标是编写一个Dapper查询,该查询将使用内部联接在返回的每个Customer中填充整个Address属性。
这是我所拥有的并且正在运行,但是我想知道这是否是最干净/最简单的方法:
StringBuilder sql = new StringBuilder();
using (var conn = GetOpenConnection())
{
sql.AppendLine("SELECT c.Id, c.Name, c.AddressId, a.Address1, a.Address2, a.City, a.State, a.ZipCode ");
sql.AppendLine("FROM Customer c ");
sql.AppendLine("INNER JOIN Address a ON c.AddressId = a.Id ");
return conn.Query<Customer, Address, Customer>(
sql.ToString(),
(customer, address) => {
customer.Address= address;
return userRole;
},
splitOn: …
Run Code Online (Sandbox Code Playgroud) 我是Dapper的新生儿.尝试将CRUD操作与Dapper和Dapper.SimpleCRUD lib结合使用.以下是示例代码...
我的数据模型看起来像
Class Product
{
public string prodId {get;set;}
public string prodName {get;set;}
public string Location {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
Dapper实现 - 插入
public void Insert(Product item)
{
using(var con = GetConnection())
{
con.Insert(item);
}
}
Run Code Online (Sandbox Code Playgroud)
由于Db中的ProdId是一个标识列,它失败了.它如何表明ProdId是DB中的标识列?
精巧的实施 - 获取
public IEnumerable<Product> GetAll()
{
IEnumerable<Product> item = null;
using (var con = GetConnection())
{
item = con.GetList<Product>();
}
return item;
}
Run Code Online (Sandbox Code Playgroud)
它给出了一个例外:
"实体必须至少有一个[Key]属性"!
我刚开始玩DapperExtensions,它看起来很有前途.但是,我对如何处理注册ClassMapper子类感到困惑.
我有一个自定义的PluralizedAutoClassMapper和一个常规的ClassMapper,我正在尝试使用它们.
这是我的复数映射器......
public class CustomPluralizedMapper<T> : PluralizedAutoClassMapper<T>
where T : class
{
private readonly Type[] SinglularTablePocoTypes = new []{
typeof(LibraryInfo)
};
public override void Table(string tableName)
{
base.Table(tableName);
if(SinglularTablePocoTypes.Any(type => string.Equals(type.Name, tableName, StringComparison.CurrentCultureIgnoreCase)))
TableName = tableName;
}
}
Run Code Online (Sandbox Code Playgroud)
...这里是专门用于LibraryInfo类的映射器
public class LibraryInfoMapper : ClassMapper<LibraryInfo>
{
public LibraryInfoMapper()
{
Map(libraryInfo => libraryInfo.Name).Column("LibraryName");
Map(libraryInfo => libraryInfo.Description).Column("LibraryDescription");
AutoMap();
}
}
Run Code Online (Sandbox Code Playgroud)
PluralizedAutoClassMapper我通过调用以下内容来开始工作......
DapperExtensions.DapperExtensions.DefaultMapper = typeof(CustomPluralizedMapper<>);
Run Code Online (Sandbox Code Playgroud)
但我不知道如何同时使用另一个.我错过了什么?
我使用下划线字符命名 MYSQL 表和列:
this_is_a_table
应映射到:ThisIsATable
this_is_a_column
应映射到:ThisIsAColumn
如果我设置的话,Dapper 可以处理这个映射:
DefaultTypeMap.MatchNamesWithUnderscores = true;
Run Code Online (Sandbox Code Playgroud)
有什么方法可以在 Dapper-Extensions 中启用此功能,以便它自动映射 undescore 吗?
我正在尝试使用Dapper
和将数据库Dapper-Extensions
序列化为.enums
string
现在,它们被序列化为整数(在VARCHAR
字段内).
有没有办法做到这一点?我可以添加的任何自定义类型映射?
如果我不能通过它,我可能需要回到EF.
我使用Dapper Extensions(DE)作为ORM.它在使用Repository模式实现的数据访问层中使用.SQL Express是后端RDBMS.
DE会自动为我生成大部分查询.我想记录这些自动生成的查询以进行调试.
我可以通过两种方式来实现这一目标: -
如何在不使用任何其他日志记录工具的情况下记录/获取Dapper Extensions自动生成的SQL查询?
在其他类似的问题是关于小巧玲珑.这个问题是关于Dapper Extensions.
dapper ×9
c# ×7
.net ×1
foreign-keys ×1
micro-orm ×1
mysql ×1
orm ×1
primary-key ×1
relation ×1
sql ×1