I've written this code to project one to many relation but it's not working:
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
IEnumerable<Store> stores = connection.Query<Store, IEnumerable<Employee>, Store>
(@"Select Stores.Id as StoreId, Stores.Name,
Employees.Id as EmployeeId, Employees.FirstName,
Employees.LastName, Employees.StoreId
from Store Stores
INNER JOIN Employee Employees ON Stores.Id = Employees.StoreId",
(a, s) => { a.Employees = s; return a; },
splitOn: "EmployeeId");
foreach (var store in stores)
{
Console.WriteLine(store.Name);
}
}
Can anybody spot the mistake?
EDIT:
These are my entities:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public double Price { get; set; }
public IList<Store> Stores { get; set; }
public Product()
{
Stores = new List<Store>();
}
}
public class Store
{
public int Id { get; set; }
public string Name { get; set; }
public IEnumerable<Product> Products { get; set; }
public IEnumerable<Employee> Employees { get; set; }
public Store()
{
Products = new List<Product>();
Employees = new List<Employee>();
}
}
EDIT:
I change the query to:
IEnumerable<Store> stores = connection.Query<Store, List<Employee>, Store>
(@"Select Stores.Id as StoreId ,Stores.Name,Employees.Id as EmployeeId,Employees.FirstName,
Employees.LastName,Employees.StoreId from Store Stores INNER JOIN Employee Employees
ON Stores.Id = Employees.StoreId",
(a, s) => { a.Employees = s; return a; }, splitOn: "EmployeeId");
and I get rid of exceptions! However, Employees are not mapped at all. I am still not sure what problem it had with IEnumerable<Employee>
in first query.
Here is another method:
Order (one) - OrderDetail (many)
Source: http://dapper-tutorial.net/result-multi-mapping#example---query-multi-mapping-one-to-many
A slight modification of Andrew's answer that utilizes a Func to select the parent key instead of
GetHashCode
.Example usage
Here is a crude workaround
its by no means the most efficient way, but it will get you up and running. I'll try and optimise this when i get a chance.
use it like this:
bear in mind your objects need to implement
GetHashCode
, perhaps like this:I wanted to keep it as simple as possible, my solution:
I still do one call to the database, and while i now execute 2 queries instead of one, the second query is using a INNER join instead of a less optimal LEFT join.
This post shows how to query a highly normalised SQL database, and map the result into a set of highly nested C# POCO objects.
Ingredients:
The insight that allowed me to solve this problem is to separate the
MicroORM
frommapping the result back to the POCO Entities
. Thus, we use two separate libraries:Essentially, we use Dapper to query the database, then use Slapper.Automapper to map the result straight into our POCOs.
Advantages
List<MyClass1>
which in turn containsList<MySubClass2>
, etc).inner joins
to return flat results is much easier than creating multiple select statements, with stitching on the client side.Disadvantages
inner join
(which brings back duplicates), we should instead use multipleselect
statements and stitch everything back together on the client side (see the other answers on this page).Performance Testing
In my tests, Slapper.Automapper added a small overhead to the results returned by Dapper, which meant that it was still 10x faster than Entity Framework, and the combination is still pretty darn close to the theoretical maximum speed SQL + C# is capable of.
In most practical cases, most of the overhead would be in a less-than-optimum SQL query, and not with some mapping of the results on the C# side.
Performance Testing Results
Total number of iterations: 1000
Dapper by itself
: 1.889 milliseconds per query, using3 lines of code to return the dynamic
.Dapper + Slapper.Automapper
: 2.463 milliseconds per query, using an additional3 lines of code for the query + mapping from dynamic to POCO Entities
.Worked Example
In this example, we have list of
Contacts
, and eachContact
can have one or morephone numbers
.POCO Entities
SQL Table
TestContact
SQL Table
TestPhone
Note that this table has a foreign key
ContactID
which refers to theTestContact
table (this corresponds to theList<TestPhone>
in the POCO above).SQL Which Produces Flat Result
In our SQL query, we use as many
JOIN
statements as we need to get all of the data we need, in a flat, denormalized form. Yes, this might produce duplicates in the output, but these duplicates will be eliminated automatically when we use Slapper.Automapper to automatically map the result of this query straight into our POCO object map.C# code
Output
POCO Entity Hierarchy
Looking in Visual Studio, We can see that Slapper.Automapper has properly populated our POCO Entities, i.e. we have a
List<TestContact>
, and eachTestContact
has aList<TestPhone>
.Notes
Both Dapper and Slapper.Automapper cache everything internally for speed. If you run into memory issues (very unlikely), ensure that you occasionally clear the cache for both of them.
Ensure that you name the columns coming back, using the underscore (
_
) notation to give Slapper.Automapper clues on how to map the result into the POCO Entities.Ensure that you give Slapper.Automapper clues on the primary key for each POCO Entity (see the lines
Slapper.AutoMapper.Configuration.AddIdentifiers
). You can also useAttributes
on the POCO for this. If you skip this step, then it could go wrong (in theory), as Slapper.Automapper would not know how to do the mapping properly.Update 2015-06-14
Successfully applied this technique to a huge production database with over 40 normalized tables. It worked perfectly to map an advanced SQL query with over 16
inner join
andleft join
into the proper POCO hierarchy (with 4 levels of nesting). The queries are blindingly fast, almost as fast as hand coding it in ADO.NET (it was typically 52 milliseconds for the query, and 50 milliseconds for the mapping from the flat result into the POCO hierarchy). This is really nothing revolutionary, but it sure beats Entity Framework for speed and ease of use, especially if all we are doing is running queries.Update 2016-02-19
Code has been running flawlessly in production for 9 months. The latest version of
Slapper.Automapper
has all of the changes that I applied to fix the issue related to nulls being returned in the SQL query.Update 2017-02-20
Code has been running flawlessly in production for 21 months, and has handled continuous queries from hundreds of users in a FTSE 250 company.
Slapper.Automapper
is also great for mapping a .csv file straight into a list of POCOs. Read the .csv file into a list of IDictionary, then map it straight into the target list of POCOs. The only trick is that you have to add a properyint Id {get; set}
, and make sure it's unique for every row (or else the automapper won't be able to distinguish between the rows).See: https://github.com/SlapperAutoMapper/Slapper.AutoMapper
According to this answer there is no one to many mapping support built into Dapper.Net. Queries will always return one object per database row. There is an alternative solution included, though.