Multi-mapping to the same table twice - dapper

How do you use Dapper's Multi-Mapping feature on two fields using the same table? i.e ClientInfo has two Address objects.
public class ClientInfo
{
public Guid Id => Guid.NewGuid();
public string FirstName { get; set; }
public string LastName { get; set; }
public Address PostalAddress { get; set; }
public Address BillingAddress { get; set; }
public int ContactNumber { get; set; }
}
public class Address
{
public Guid Id = Guid.NewGuid();
public string FirstLine { get; set; }
public string SecondLine { get; set; }
public string Town { get; set; }
public string PostCode { get; set; }
}
Tables
Relational - Address.Id used in ClientInfo.PostalAddress / BillingAddress
tbl.Address
|Id|FirstLine|SecondLine|Town|PostCode
tbl.ClientInfo
|Id|FirstName|LastName|PostalAddress|BillingAddress|etc..
Current implementation
Results only in all but PostalAddress being mapped.
var sql = #"select * from ClientInfo c left join Address as a on a.Id = c.PostalAddress left join Address as ad on ad.Id = c.BillingAddress";
var clients = connection.Query<ClientInfo, Address, Address, ClientInfo>(
sql,
(client, postal, billing) =>
{
client.PostalAddress = postal;
client.BillingAddress = billing;
return client;
},
splitOn: "PostalAddress,BillingAddress")
.Distinct()
.ToList();
return clients;

The splitOn parameter tells Dapper when/where to start mapping the next object, so you need to ensure that your SQL query returns the information in the correct order. Right now, you return 2 guids for PostalAddress and BillingAddress. Dapper doesn't know how to map them both.
select * from ... join ... will result in the Address data ordered AFTER the ClientInfo.PostalAddress and ClientInfo.BillingAddress columns.
Try: SELECT c.Id, c.FirstName, c.LastName, c.ContactNumber, a.*, ad.* FROM ClientInfo c LEFT JOIN Address AS a ON a.Id = c.PostalAddress JOIN Address AS ad ON ad.Id = c.BillingAddress
As you can see, removing the * effectively excludes the PostalAddress and BillingAddress guids from the results and we can now now splitOn: "Id,Id".

You will of course not have to provide the GUIDs in the select statement, this is just for the test to work.
[Test]
public void TestAbcd()
{
using (var dbConnection = new SqlConnection(_connectionString))
{
const string sql = #"WITH ClientInfo AS (
SELECT * FROM (
VALUES (#ci1, #adr1, #adr2), (#ci1, #adr3, #adr4)
) AS a (Id, PostalAddress, BillingAddress)
),
Address AS (
SELECT * FROM (
VALUES
(#adr1), (#adr2), (#adr3), (#adr4)
) AS a (Id)
)
select * from ClientInfo c left join Address as a on a.Id = c.PostalAddress left join Address as ad on ad.Id = c.BillingAddress";
dbConnection.Open();
var clients = dbConnection.Query<ClientInfo, Address, Address, ClientInfo>(
sql,
(client, postal, billing) =>
{
client.PostalAddress = postal;
client.BillingAddress = billing;
return client;
},
splitOn: "PostalAddress,BillingAddress", param: new {
ci1 = Guid.NewGuid(),
ci2 = Guid.NewGuid(),
adr1 = Guid.NewGuid(),
adr2 = Guid.NewGuid(),
adr3 = Guid.NewGuid(),
adr4 = Guid.NewGuid()
})
.Distinct()
.ToList();
}
}

Related

Dapper multi object query One-Many

Not really sure why I'm not getting the child object populated.
My tables:
Product:
[ProductId]
,[Brand]
,[Model]
StoreProduct:
[StoreId]
,[ProductId]
,[StoreProductId]
Class
public class Product
{
public Guid ProductId { get; set; }
public string Brand { get; set; }
public string Model { get; set; }
public virtual List<StoreProduct> StoreProducts { get; set; }
}
public class StoreProduct
{
public int StoreId { get; set; } //Key 0
public Guid ProductId { get; set; } //Key 1
public Store Store { get; set; }
public Product Product { get; set; }
public string StoreProductId { get; set; } //A new Id specific for each store
}
My Dapper Code
string sql = "SELECT * FROM StoreProduct AS A INNER JOIN Product AS B ON A.ProductId = B.ProductId WHERE A.StoreProductId = #StoreProductId and A.StoreId = #StoreId";
var connection = AppDbContext.Database.GetDbConnection();
return connection.Query<StoreProduct, Product, Product>(
sql,
(StoreProduct, Product) => { StoreProduct.ProductId = Product.ProductId; return Product; },
new { StoreProductId = storeProductId, StoreId = StoreID }, splitOn: "ProductId")
.FirstOrDefault();
What the DB returns:
But... StoreProducts List is null.
Use Dapper the way it works.
var listProduct = new Dictionary<string, Product>();
var listStoreProduct = new Dictionary<string, StoreProduct>();
using var connection = _connectionProvider.GetConnection();
var query = "SELECT * FROM StoreProduct AS A INNER JOIN Product AS B ON A.ProductId = B.ProductId WHERE A.StoreProductId = #StoreProductId and A.StoreId = #StoreId";
var result = connection.Query<Product, StoreProduct, Product>(query, (product, storeProduct) =>
{
if (!listProduct.TryGetValue(product.Id, out var entry))
{
entry = product;
listProduct.Add(entry.Id, entry);
}
if (storeProduct != null && !listStoreProduct.TryGetValue(storeProduct.Id, out var procInstance))
{
listStoreProduct.Add(procInstance.Id, procInstance);
entry.ProcessInstance.Add(procInstance);
}
return entry;
}, splitOn: "ProductId").Distinct().ToList();
I hope I could have helped you.

Problem with NewtonJson deserialization json from stored procedure which return result for json

I have a stored procedure in SQL Server 2016 like this:
SELECT
a.Id as [ProdList.ProdId],
#Main1 as [ProdList.Main],
a.Hit as [ProdList.Hit],
a.New as [ProdList.New],
a.Sale as [ProdList.Sale],
a.Price as [ProdList.Price],
a.Price_old as [ProdList.OldPrice],
a.Sort as [ProdList.Sort],
a.ShName as [ProdList.Name],
c.Big as [ProdList.ImagePath],
a.SaleSize as [ProdList.SaleSize],
'' as [ProdList.QueryString],
0 as [ProdList.RANK],
(SELECT
('/products/product/'+cast(f.Id as nvarchar(255))) as [Link],
g.Big as [Img]
FROM
Products f
INNER JOIN
GroupProducts d ON d.ProdId = f.Id
INNER JOIN
ProdImages g ON g.ProdId = f.Id
WHERE
d.ParentId = a.Id AND g.Main = 1
FOR JSON PATH) AS [GroupProduct]
FROM
Products a
INNER JOIN
ProdInCategory b ON a.Id = b.ProdId
INNER JOIN
ProdImages c ON a.Id = c.ProdId
INNER JOIN
GroupProducts e ON a.Id = e.ProdId
WHERE
c.Main = 1 AND b.CatId = #CatId AND e.ParentId = 0
UNION
SELECT
a.Id as [ProdList.ProdId],
#Main1 as [ProdList.Main],
a.Hit as [ProdList.Hit],
a.New as [ProdList.New],
a.Sale as [ProdList.Sale],
a.Price as [ProdList.Price],
a.Price_old as [ProdList.OldPrice],
a.Sort as [ProdList.Sort],
a.ShName as [ProdList.Name],
g.Big as [ProdList.ImagePath],
a.SaleSize as [ProdList.SaleSize],
'' as [ProdList.QueryString],
0 as [ProdList.RANK],
null as [GroupProduct]
FROM
Products a
INNER JOIN
ProdImages g ON g.ProdId = a.Id
INNER JOIN
ProdInCategory pc ON a.Id = pc.ProdId
WHERE
a.Id NOT IN (SELECT ProdId FROM GroupProducts)
AND pc.CatId = #CatId
FOR JSON PAT
In my ASP.NET MVC 5 application I run this stored procedure and deserialize result to object:
string query = String.Format("EXEC [dbo].[GetListProdId3] #CatId={0}", incommingModel.id);
var result = string.Concat(db.Database.SqlQuery<string>(query));
List<ProdList> tovarListJson1 = JsonConvert.DeserializeObject<List<ProdList>>(result);
The first select to union takes all the row filled with GroupProduct. The second select after the union takes the rest of the products from the category that have no group (Not in GroupProduct). If Im comment out half of the selection before the union statement in SP, then everything works(Independently before union select top or bottom). If Im try to run the whole select with the union statement, I get the error:
My class to deserialization :
public class ProdList
{
public int ProdId { get; set; }
public bool Main { get; set; }
public bool Hit { get; set; }
public bool New { get; set; }
public bool Sale { get; set; }
public decimal Price { get; set; }
public decimal OldPrice { get; set; }
public decimal Sort { get; set; }
public string Name { get; set; }
public string ImagePath { get; set; }
public string SaleSize { get; set; }
public int Rank { get; set; }
public string QueryString { get; set; }
public List<NestedProdList> GroupProduct { get; set; }
}
My json from stored procedure(first element and last):
[{"ProdList":{"ProdId":225,"Main":true,"Hit":false,"New":false,"Sale":false,"Price":22.0000,"OldPrice":0.0000,"Sort":23.5200,"Name":"Ручка шариковая \"Tris Lx\", синяя","ImagePath":"\/images2\/44012\/ruchka-sharikovaya-tris-1.jpg","SaleSize":"0% ","QueryString":"","RANK":0},"GroupProduct":"[{\"Link\":\"\\\/products\\\/product\\\/23091\",\"Img\":\"\\\/images2\\\/40554\\\/ruchka-sharikovaya-tris-1.jpg\"},{\"Link\":\"\\\/products\\\/product\\\/32836\",\"Img\":\"\\\/images2\\\/40555\\\/ruchka-sharikovaya-tris-1.jpg\"},{\"Link\":\"\\\/products\\\/product\\\/32918\",\"Img\":\"\\\/images2\\\/44010\\\/ruchka-sharikovaya-tris-1.jpg\"},{\"Link\":\"\\\/products\\\/product\\\/37093\",\"Img\":\"\\\/images2\\\/44011\\\/ruchka-sharikovaya-tris-1.jpg\"},{\"Link\":\"\\\/products\\\/product\\\/56573\",\"Img\":\"\\\/images2\\\/40553\\\/ruchka-sharikovaya-tris-1.jpg\"}]"},{"ProdList":{"ProdId":68812,"Main":true,"Hit":false,"New":false,"Sale":false,"Price":9.2000,"OldPrice":20.8900,"Sort":21.5600,"Name":"MIR FANTASY, ручка шариковая","ImagePath":"\/images2\/63849\/mir-fantasy-ruchka-1.jpg","SaleSize":"0% ","QueryString":"","RANK":0},"GroupProduct":null},{"ProdList":{"ProdId":69329,"Main":true,"Hit":false,"New":false,"Sale":false,"Price":27.0000,"OldPrice":0.0000,"Sort":31.3600,"Name":"Ручка шариковая \"Twisty Safe Touch\", светло-голубая","ImagePath":"\/images2\/11763\/ruchka-sharikovaya-twisty-1.jpg","SaleSize":"0% ","QueryString":"","RANK":0},"GroupProduct":null}]

Dapper Multi Mapping Result

I have two classes
public class Customer
{
public int CustomerId { get; set;}
public string CustomerName { get; set; }
}
public class Order
{
public int OrderId { get; set; }
public int CustomerId { get; set; } //BuyerCustomer
public int CustomerSecondId { get; set; } //ReceiverCustomer
public Customer BuyerCustomer { get; set; }
public Customer ReceiverCustomer { get; set; }
}
Here's my query will look like
SELECT a.*, b.*, c.* FROM dbo.PRODUCTS_ORDER a
INNER JOIN dbo.CUSTOMER b ON a.CustomerId=b.CustomerId
INNER JOIN dbo.CUSTOMER c ON a.CustomerSecondId=b.CustomerId
Dapper Implementation..
List<Order> order= null;
order= (List<Order>)dapperconnection.Query<Order, Customer, Customer, Order>(sql,
(order, customer1,customer2) =>
{
order.BuyerCustomer = customer1;
order.ReceiverCustomer = customer2;
return order;
}, splitOn: "CustomerId,CustomerSecondId ");
The result I'm getting is incomplete, only the RecevierCustomer gets populated while the BuyerCustomer doesn't contain any values at all.
It looks like dapper is confused since i used the CustomerId twice in my query.
Is there any workaround with this without having to change my the Customer class?
There are few issues with your class design and Dapper query.
Customer.CustomerName should be string
I would remove CustomerId and CustomerSecondId from Order. They are redundant. You have both Id's in the Customer.
Remove CustomerSecondId from Split.
Below is a working test:
public class Order
{
public int OrderId { get; set; }
public Customer BuyerCustomer { get; set; }
public Customer ReceiverCustomer { get; set; }
}
public class Customer
{
public int CustomerId { get; set; }
public string CustomerName { get; set; }
}
[Test]
public void TestSplitMany()
{
using (var conn = new SqlConnection(#"Data Source=.\sqlexpress;Integrated Security=true; Initial Catalog=foo"))
{
var result =
conn.Query<Order, Customer, Customer, Order>(#"select OrderId = 1, CustomerId = 2, CustomerName = 'Jim', CustomerId = 3, CustomerName = 'Bob'",
(order, buyer, receiver) =>
{
order.BuyerCustomer = buyer;
order.ReceiverCustomer = receiver;
return order;
}, splitOn: "CustomerId").First();
Assert.That(result.BuyerCustomer.CustomerId == 2);
Assert.That(result.ReceiverCustomer.CustomerId == 3);
Assert.That(result.BuyerCustomer.CustomerName == "Jim");
Assert.That(result.ReceiverCustomer.CustomerName == "Bob");
}
}

Dapper Multi-map next level

I'm using multiple mapping for a current query and now I need to map another object on the initial query.
For example:
public class Part {
public int Id { get; set; }
public string Name { get; set; }
public Address Address { get; set; }
}
public class Address {
public int Id { get; set; }
public string Street { get; set; }
public SiteOu Ou { get; set; }
}
public class SiteOu
public int Id { get; set; }
public string Name { get; set; }
}
Dapper:
connection.Query<Part, Address, Part>(sql, (part, address) => {
part.Address = address;
});
How do I get the Address class to have the SiteOu information?
This example isn't what I'm actually doing because I've actually got
Query<T1,T2,T3,T4,T5,TResult>();
I'm doing 1 select and 5 joins in my query. So hopefully I don't need more overloads of Query.
Dapper allows you to map a single row to multiple objects, so you can just map SiteOu as part of the same query.
[Test]
public void TestSplitOn()
{
var conn = new SqlConnection(#"Data Source=.\SQLEXPRESS;Integrated Security=true;Initial Catalog=db");
conn.Open();
const string sql = "select Id = 1, Name = 'My Part', " +
"Id = 2, Street = 'My Street', " +
"Id = 3, Name = 'My Site'";
var result = conn.Query<Part, Address, SiteOu, Part>(sql, (part, address, siteOu) =>
{
part.Address = address;
address.Ou = siteOu;
return part;
},
commandType: CommandType.Text
).FirstOrDefault();
Assert.That(result, Is.Not.Null);
Assert.That(result.Address, Is.Not.Null);
Assert.That(result.Address.Ou, Is.Not.Null);
}
Important Note: Dapper assumes your Id columns are named "Id" or "id", if your primary key is different or you would like to split the wide row at point other than "Id", use the optional 'splitOn' parameter.
If you have more that 5 types to map, another out of the box option is to use QueryMultiple extension. Here is an example from the Dapper docs.
var sql =
#"
select * from Customers where CustomerId = #id
select * from Orders where CustomerId = #id
select * from Returns where CustomerId = #id";
using (var multi = connection.QueryMultiple(sql, new {id=selectedId}))
{
var customer = multi.Read<Customer>().Single();
var orders = multi.Read<Order>().ToList();
var returns = multi.Read<Return>().ToList();
...
}
Also check out this thread.

Dapper one to many losing Id in the many

Given the following two entities
public class Customer
{
public Customer()
{
Orders = new List<Orders>();
}
public int CustomerId { get; set; }
public string CustomerName { get; set; }
public List<Orders> Orders { get; set; }
}
and
public class Orders
{
public int OrderId { get; set; }
public int CustomerId { get; set; }
public int Quantity { get; set; }
public string OrderDescription { get; set; }
}
And the following code
string sql = #"
select c.CustomerId, c.CustomerName, o.OrderId as OrderId, o.CustomerId, o.Quantity, o.OrderDescription
from Customer c
join Orders o on c.CustomerId = o.CustomerId";
var customers = new List<Customer>();
Customer currentCustomer = null;
var c = connection.Query<Customer, Orders, Customer>(
sql, (customer, order) => {
if (currentCustomer == null || currentCustomer.CustomerId != customer.CustomerId)
{
customers.Add(customer);
currentCustomer = customer;
}
currentCustomer.Orders.Add(order);
return currentCustomer;
}, splitOn: "CustomerId, OrderId");
When I inspect customers the OrderId is always 0. As in customers.Orders[0].OrderId is zero, for all of them. Now I suppose it is me doing something wrong, but I can't figure out what. The strange SQL you see above is my attempts to try and force the OrderId to work.

Resources