我尝试将我的 ID 字段映射到列属性,但出于某种原因,这似乎不起作用,我也不知道为什么。我设置了一个测试项目来展示我正在尝试的内容。
首先,我得到了我的 2 个实体:
实体表1
using System.Data.Linq.Mapping;
namespace DapperTestProj
{
public class Table1
{
[Column(Name = "Table1Id")]
public int Id { get; set; }
public string Column1 { get; set; }
public string Column2 { get; set; }
public Table2 Table2 { get; set; }
public Table1()
{
Table2 = new Table2();
}
}
}
和实体表2
using System.Data.Linq.Mapping;
namespace DapperTestProj
{
public class Table2
{
[Column(Name = "Table2Id")]
public int Id { get; set; }
public string Column3 { get; set; }
public string Column4 { get; set; }
}
}
在我的数据库中,我有 2 个表,也分别命名为 Table1 和 Table2。除了 Table1 有一个名为 Table2Id 的列并且 Table1.Table2Id 和 Table2.Id 之间还有一个外键外,这两个表的列名称都与实体相同。
此外,两个表中各有 1 条记录,并且 Id 均为 2。
接下来我尝试使用 dapper 执行查询,它应该返回一个 Table1 类型的对象。这有效,但属性 Table1.Id 和 Table1.Table2.Id 都保持为 0(默认整数)。我希望列属性会映射 Id 字段,但显然这并不令人满意。
这是我在代码中执行的查询和映射:
private Table1 TestMethod(IDbConnection connection)
{
var result = connection.Query<Table1, Table2, Table1>(
@"SELECT
T1.Id as Table1Id,
T1.Column1 as Column1,
T1.Column2 as Column2,
T2.Id as Table2Id,
T2.Column3 as Column3,
T2.Column4 as Column4
FROM Table1 T1
INNER JOIN Table2 T2 ON T1.Table2Id = T2.Id",
(table1, table2) =>
{
table1.Table2 = table2;
return table1;
},
splitOn: "Table2Id"
).SingleOrDefault();
return result;
}
现在我可以将实体中的两个 Id 属性字段重命名为 Table1Id 和 Table2Id,但我更喜欢 Id,因为逻辑代码更多,例如 Table1.Id 而不是 Table1.Table1Id。所以我想知道,是否有可能是我想要的,如果是,如何实现?
编辑:
我找到了这个主题: Manually Map column names with class properties
使用 Kaleb Pederson 第一篇文章中的代码,可以在需要时使用 FallBackTypeMapper 类和 ColumnAttributeTypeMapper 类的属性。所需要做的就是将所需的类添加到类型映射中:
SqlMapper.SetTypeMap(typeof(Table1), new ColumnAttributeTypeMapper<Table1>());
SqlMapper.SetTypeMap(typeof(Table2), new ColumnAttributeTypeMapper<Table2>());
但是对于许多实体来说,这个列表会变得很长。您还需要手动将每个类添加到列表中,我想知道这是否可以通过反射自动完成并更通用。我找到了一个能够获取所有类型的代码片段:
const string @namespace = "DapperTestProj.Entities";
var types = from type in Assembly.GetExecutingAssembly().GetTypes()
where type.IsClass && type.Namespace == @namespace
select type;
遍历所有类型,我可以做到这一点,我现在唯一的问题是我现在需要拥有或需要放在问号所在位置的代码片段是什么?
typeList.ToList().ForEach(type => SqlMapper.SetTypeMap(type,
new ColumnAttributeTypeMapper</*???*/>()));
编辑:
经过更多的搜索,我找到了我最后一个问题的解决方案:
typeList.ToList().ForEach(type =>
{
var mapper = (SqlMapper.ITypeMap)Activator.CreateInstance(
typeof(ColumnAttributeTypeMapper<>)
.MakeGenericType(type));
SqlMapper.SetTypeMap(type, mapper);
});
最佳答案
为了完成解决方案,我想与感兴趣的人分享我找到并整理的代码。
而不是(ab)使用 System.Data.Linq.Mapping.ColumnAttribute,它可能更符合逻辑(并且可能保存,尽管 Microsoft 将 linq 更改为 sql ColumnAttribute 类的机会非常小)来创建我们自己的 ColumnAttribute 类:
ColumnAttribute.cs
using System;
namespace DapperTestProj.DapperAttributeMapper //Maybe a better namespace here
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class ColumnAttribute : Attribute
{
public string Name { get; set; }
public ColumnAttribute(string name)
{
Name = name;
}
}
}
在我前面提到的主题中找到了 FallBackTypeMapper 和 ColumnAttributeTypeMapper 类:
FallBackTypeMapper.cs
using System;
using System.Collections.Generic;
using System.Reflection;
using Dapper;
namespace DapperTestProj.DapperAttributeMapper
{
public class FallBackTypeMapper : SqlMapper.ITypeMap
{
private readonly IEnumerable<SqlMapper.ITypeMap> _mappers;
public FallBackTypeMapper(IEnumerable<SqlMapper.ITypeMap> mappers)
{
_mappers = mappers;
}
public ConstructorInfo FindConstructor(string[] names, Type[] types)
{
foreach (var mapper in _mappers)
{
try
{
var result = mapper.FindConstructor(names, types);
if (result != null)
{
return result;
}
}
catch (NotImplementedException nix)
{
// the CustomPropertyTypeMap only supports a no-args
// constructor and throws a not implemented exception.
// to work around that, catch and ignore.
}
}
return null;
}
public SqlMapper.IMemberMap GetConstructorParameter(ConstructorInfo constructor, string columnName)
{
foreach (var mapper in _mappers)
{
try
{
var result = mapper.GetConstructorParameter(constructor, columnName);
if (result != null)
{
return result;
}
}
catch (NotImplementedException nix)
{
// the CustomPropertyTypeMap only supports a no-args
// constructor and throws a not implemented exception.
// to work around that, catch and ignore.
}
}
return null;
}
public SqlMapper.IMemberMap GetMember(string columnName)
{
foreach (var mapper in _mappers)
{
try
{
var result = mapper.GetMember(columnName);
if (result != null)
{
return result;
}
}
catch (NotImplementedException nix)
{
// the CustomPropertyTypeMap only supports a no-args
// constructor and throws a not implemented exception.
// to work around that, catch and ignore.
}
}
return null;
}
}
}
ColumnAttributeTypeMapper.cs
using System.Linq;
using Dapper;
namespace DapperTestProj.DapperAttributeMapper
{
public class ColumnAttributeTypeMapper<T> : FallBackTypeMapper
{
public ColumnAttributeTypeMapper()
: base(new SqlMapper.ITypeMap[]
{
new CustomPropertyTypeMap(typeof(T),
(type, columnName) =>
type.GetProperties().FirstOrDefault(prop =>
prop.GetCustomAttributes(false)
.OfType<ColumnAttribute>()
.Any(attribute => attribute.Name == columnName)
)
),
new DefaultTypeMap(typeof(T))
})
{
}
}
}
最后,TypeMapper.cs 用于初始化映射。
using System;
using System.Linq;
using System.Reflection;
using Dapper;
namespace DapperTestProj.DapperAttributeMapper
{
public static class TypeMapper
{
public static void Initialize(string @namespace)
{
var types = from assem in AppDomain.CurrentDomain.GetAssemblies().ToList()
from type in assem.GetTypes()
where type.IsClass && type.Namespace == @namespace
select type;
types.ToList().ForEach(type =>
{
var mapper = (SqlMapper.ITypeMap)Activator
.CreateInstance(typeof(ColumnAttributeTypeMapper<>)
.MakeGenericType(type));
SqlMapper.SetTypeMap(type, mapper);
});
}
}
}
启动时需要调用TypeMapper.Initialize:
TypeMapper.Initialize("DapperTestProj.Entities");
您可以开始使用实体属性的属性
using DapperTestProj.DapperAttributeMapper;
namespace DapperTestProj.Entities
{
public class Table1
{
[Column("Table1Id")]
public int Id { get; set; }
public string Column1 { get; set; }
public string Column2 { get; set; }
public Table2 Table2 { get; set; }
public Table1()
{
Table2 = new Table2();
}
}
}
关于c# - 具有属性映射的 Dapper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20951531/