我有以下情况。一个项目,其数据库包含所有带有名为“ id”的主键的表。执行联接以加载子类时,我使用类似下面的代码:
联接查询示例:
var sql1 = @ "SELECT
t.id AS id_ticket_entity,
t.statusticketid,
t.usuarioclienteid,
t.categoriaid,
t.classification,
title,
t.description,
t.data_hora_inicial,
t.data_hora_alteracao,
CASE
WHEN t.data_hora_ultima_messagem IS NULL THEN t.data_hora_inicial
WHEN t.data_hora_ultima_mensagem IS NOT NULL THEN t.data_hora_ultima_mensagem
END AS date_host_message_message,
t.data_time_final,
ct.id AS id_categoria_entity,
ct.name,
cl.id AS id_classificacao_entity,
cl.name,
st.id AS id_status_ticket_entity,
st.name,
st.use,
it's open,
it's open,
uc.id AS user_entity_entity,
ucl.clienteid,
uc.name,
uc.email,
uc.phonephone,
uc.cellphone,
uc.copy,
uc.provisorio,
uactive,
c.id AS id_cliente_entity,
c.name
FROM dbo.Ticket t
LEFT JOIN dbo.Category ct ON t.categoriaid = ct.id
LEFT JOIN dbo.classification cl ON t.classificationid = cl.id
LEFT JOIN dbo.Status_Ticket st ON t.statusticketid = st.id
LEFT JOIN dbo.Usuario_Customer uc ON t.usuarioclienteid = uc.id
LEFT JOIN dbo.Customer c ON uc.clienteid = c.id
WHERE t.id = "+ idTicket;
我如何执行上述查询的示例:
result1 = conn.Query <Ticket, Category, Classification, StatusTicket, CustomerCustomer, Client, Ticket> (sql1,
map: (ticket, category, rating, status, user, client) =>
{
ticket.Category = category;
ticket.Classification = classification;
ticket.StatusTicket = status;
ticket.UsuarioCustomer = user;
ticket.UsuarioCliente.Cliente = customer;
return ticket;
},
splitOn: "id_categoria_entity, id_classificacao_entity, id_status_ticket_entity, id_usario_cliente_entity, id_cliente_entity"). SingleOrDefault ();
以下是其中的某些实体使用的FluentMap:
public TicketMapping()
{
ToTable("Ticket");
Map(m => m.Id).ToColumn("id_ticket_entity").IsKey().IsIdentity();
Map(m => m.IdStatusTicket).ToColumn("statusticketid");
Map(m => m.IdUsuarioCliente).ToColumn("usuarioclienteid");
Map(m => m.IdCategoria).ToColumn("categoriaid");
Map(m => m.IdClassificacao).ToColumn("classificacaoid");
Map(m => m.Titulo).ToColumn("titulo");
Map(m => m.Descricao).ToColumn("descricao");
Map(m => m.DataHoraInicial).ToColumn("data_hora_inicial");
Map(m => m.DataHoraAlteracao).ToColumn("data_hora_alteracao");
Map(m => m.DataHoraUltimaMensagem).ToColumn("data_hora_ultima_mensagem");
Map(m => m.DataHoraFinal).ToColumn("data_hora_final");
Map(m => m.Categoria).Ignore();
Map(m => m.Classificacao).Ignore();
Map(m => m.StatusTicket).Ignore();
Map(m => m.UsuarioCliente).Ignore();
}
public CategoriaMapping()
{
ToTable("Categoria");
Map(m => m.Id).ToColumn("id_categoria_entity").IsKey().IsIdentity();
Map(m => m.Nome).ToColumn("nome");
}
public ClassificacaoMapping()
{
ToTable("Classificacao");
Map(m => m.Id).ToColumn("id_classificacao_entity").IsKey().IsIdentity();
Map(m => m.Nome).ToColumn("nome");
}
可以看出,我在所有主键上都使用了别名,因此我可以在所选字段中确定键的位置,并允许Dapper正确加载子类。
到目前为止,显然这是可行的,但问题是我有一个通用类满足所有存储库以执行基本CRUD,并且由于ALIAS的这种映射,我的通用类停止了工作,因为我无法直接执行搜索,因为无法映射到实体的原始“ id”列,而只能映射到键的ALIAS,因此我失去了使用该通用类的便利,这迫使我不得不编写几乎所有代码所有实体的CRUD。
以下是Dapper和Dommel从CRUD的通用类进行查询的示例:
public virtual T GetById(Int32 id)
{
try
{
using (var conn = CreateConnection())
{
return conn.Get<T>(id);
}
}
catch (Exception ex)
{
return null;
}
}
我不想走这种必须编写通用类中已经准备好的代码的方法,其想法是保留通用类,并有办法解决这些联接或映射它们(如果有)不是通过别名,而是另一种方法...
更改数据库中密钥名称的选项不视为解决方案。