question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get with Include fails with multiple related entities

See original GitHub issue

I’m having trouble getting your example code to work from the wiki under the ‘Joins’ section. I’ve created the relevant database objects, etc. I have a single workstation that has multiple employee’s. However, when executing the following code:

con.Get(new Workstation {WorkstationId = workStationId },
                    statement => statement.Include<Employee>()));

An exception is thrown:

Sequence contains more than one element

Stack trace if useful:

   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
   at Dapper.FastCrud.SqlStatements.RelationshipSqlStatements`1.SelectById(IDbConnection connection, TEntity keyEntity, AggregatedSqlStatementOptions`1 statementOptions)
   at DapperCore.Repository.Repository.GetWorkstation(Int32 workStationId) in C:\W\DapperCore\DapperCore\src\DapperCore\Repository\Repository.cs:line 28
   at DapperConsole.Program.Main(String[] args) in C:\W\DapperCore\DapperCore\src\DapperConsole\Program.cs:line 23

It doesn’t seem to be able to handle the fact that the Workstation has many Employee objects associated with it.

If I remove the Employee’s from the database so that there is only a one-to-one relationship between the two. The code above works fine and I get only a single Employeeobject in the collection.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
MoonStormcommented, Oct 21, 2016

I believe I found the problem. The algorithm is creating unique entities but this information should also be used later on as Dapper still returns an entity per row. The fix should be trivial and it will be faster than simply applying Distinct.

0reactions
MoonStormcommented, Oct 27, 2016

Fixed and released. Give it a shot but be aware that Dapper is suffering from a bug that causes the async methods to fail.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Entity Framework Include() is not working
I am getting a null reference exception when I access the QuestionType property. I am using Include("QuestionType") but it doesn't appear to be ......
Read more >
Performance issue when using multiple includes and ...
I'm having a performance issue when using multiple includes with SplitQuery and QueryFilters. When I do: dbContext.Principal .Include(p => p.
Read more >
Loading Related Entities - EF6
Include is an extension method in the System.Data.Entity namespace so make sure you are using that namespace. Eagerly loading multiple levels.
Read more >
Querying in Entity Framework Core
Use the Include() method multiple times to load multiple navigation properties of the same entity. For example, the following code loads Grade and ......
Read more >
Querying data via the DbSet
The Include method is used to specify related entities that should be included in the query so that you don't have to issue...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found