- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个具有以下属性的 Customer 类:
public int Id { get; set; }
public string Name { get; set; }
public int AddressId { get; set; }
public Address Address { get; set; }
我的目标是编写一个 Dapper 查询,该查询将使用内部联接来填充每个返回的客户中的整个地址属性。
这是我所拥有的,它正在工作,但我想知道这是否是最干净/最简单的方法:
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: "AddressId"
).ToList();
}
我对添加另一个属性有些担心,例如:
public Contact Contact { get; set; }
我不确定如何切换上面的语法来填充地址和联系人。
最佳答案
我使用 Dapper 1.40 版进行编码,并且编写了如下所示的查询,我没有任何问题可以填充一个以上的对象,但我遇到了 8 个我可以映射的不同类的限制一个问题。
public class Customer {
public int Id { get; set; }
public string Name { get; set; }
public int AddressId { get; set; }
public int ContactId { get; set; }
public Address Address { get; set; }
public Contact Contact { get; set; }
}
public class Address {
public int Id { get; set; }
public string Address1 {get;set;}
public string Address2 {get;set;}
public string City {get;set;}
public string State {get;set;}
public int ZipCode {get;set;}
public IEnumerable<Customer> Customer {get;set;}
}
public class Contact {
public int Id { get; set; }
public string Name { get; set; }
public IEnumerable<Customer> Customer {get;set;}
}
using (var conn = GetOpenConnection())
{
var query = _contextDapper
.Query<Customer, Address, Contact, Customer>($@"
SELECT c.Id, c.Name,
c.AddressId, a.Id, a.Address1, a.Address2, a.City, a.State, a.ZipCode,
c.ContactId, ct.Id, ct.Name
FROM Customer c
INNER JOIN Address a ON a.Id = c.AddressId
INNER JOIN Contact ct ON ct.Id = c.ContactId",
(c, a, ct) =>
{
c.LogType = a;
c.Contact = ct;
return c;
}, splitOn: "AddressId, ContactId")
.AsQueryable();
return query.ToList();
}
关于c# - 使用 Dapper 查询复杂对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44980945/
有谁知道或有如何使用的链接https://github.com/henkmollema/Dapper-FluentMap在我的 Dapper CRUD 中?现在我正在使用 Dapper.Contrib
我正在寻找一种方法来仅更新 Dapper 中的设置属性。即仅当实体的属性不为空时才更新实体的属性。 我正在用一种相当粗糙的方法解决同样的问题,如下所示,但我相信应该有一种更简洁的方法来做到这一点。
方案: 我的模型中有一个字符串属性,其中包含MultiSelectList @Html.ListBox的ID。如果选择两个列表项,则我的属性值将类似于0100,0500。 问题: Dapper whe
我们正在使用 Dapper 进行一些数据访问事件,并使用标准推荐方法连接到数据库,如下所示: public static Func ConnectionFactory = () => new SqlC
是否可以通过表名 作为 Dapper Query 命令的参数?我不是在寻找 SQL 表定义的函数或 SQL 表变量。我想在 C# 中定义表名并将其传递给 Dapper。这是我的代码,执行时返回错误 M
我在dapper中尝试对包含NULL的列进行拆分时遇到MultiMaps问题。 Dapper似乎不实例化对象,并且我的映射函数接收null而不是对象。 这是我的新测试: class Produ
我似乎找不到我的问题的文档或示例(现在已经搜索了一段时间)。我认为我的问题很简单,所以这里是。 我有两张 table 。我的主表称为 Persons,辅助表是 PersonEntries。对于 Per
我的代码有点问题,我想让 dapper 很好地使用它。 当我说我的代码是继承的,所以这不是我的设计。 我正在尝试替换 Entity Framework,因为对数据库的调用效率不高,所以我想更好地控制生
我已经开始玩 Dapper.Net,到目前为止我真的很喜欢它 - 然而,我遇到了一个问题。 假设我有一个 POCO 类: public class Person { public string
我已经开始玩 Dapper.Net,到目前为止我真的很喜欢它 - 然而,我遇到了一个问题。 假设我有一个 POCO 类: public class Person { public string
我知道这是一种错误的做法,但我正在处理具有 NULLS 的遗留代码库,当它表示空字符串时,反之亦然。 我无法立即看到它是如何可能的,但是当从数据库映射回来时,是否有可能获取(或修改 dapper 以便
我正在尝试选择一个包含 2 个整数列的列表,将结果映射到一个元组。举个例子: return connection.Query>("select id1, id2 from sometable").To
我有一个存储过程,它有一个没有默认值的参数,但它可以为空。但我不知道如何使用 Dapper 传递 null。我可以在 ADO 中做得很好。 connection.Execute("spLMS_Upda
我决定使用 Dapper.net,因为它似乎只做我想做的事情:映射,我不需要任何花哨的东西,我只是厌倦了处理我的数据读取器和我的对象之间的映射。 我的问题: 假设我有这些类(class): class
我在从 Dapper 查询返回单个对象时遇到问题。我有一个简单的查询,我想从返回的集合中获取第一个对象。我错过了什么? using (SqlConnection sqlConnection = new
假设我的实体是 public class AppUser { string Id { get; set; } string Name { get; set; } } 看起来默认情况下
我在Dapper官方文档中看到了QueryMultiple,如下所示,很方便! var sql = @" select * from Customers where CustomerId =
我正在使用 Dapper 查询包含 XML 字段的表: CREATE TABLE Workflow ( Guid uniqueidentifier not null, State xm
我有一个来自Dapper查询的动态结果,其中包含如下记录: {DapperRow, billing_currency_code = 'USD', count(*) = '6'} 我可以使用rowVar
我正在使用 Dapper 扩展,但我的数据库中有多个架构名称。 我在下面的链接中找到了答案,但它假设我只有一个模式名称,这不是我的情况。 Dapper Extensions Change Schema
我是一名优秀的程序员,十分优秀!