1

I am having trouble including my Identity User in query results. Other entities are included just fine, no matter how many levels deep.

Here's the model I'm using.

Building * --- 1 City
     *            *
     |          /
     |         /
     1        1
ApplicationUser

And the context:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public DbSet<City> Cities { get; set; }
    public DbSet<Building> Buildings { get; set; }
}

Both Building and City have these properties:

public Guid ApplicationUserId { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }

The query I'm using to retrieve data:

var building = context.Buildings
    .Include(c => c.ApplicationUser)
    .Include(c => c.City)
    .Include(c => c.City.ApplicationUser)
    .First();

In the result City is populated just fine, however ApplicationUser is not.

Could this be a naming issue? I've already tried UserId / User and AspNetUserId / AspNetUser as property names without succes.

I'm using Migrations to create the database. The table name that gets created for users is AspNetUsers.

I'm using entity framework 7 beta 7, not sure if this applies to other versions as well.

These are the files generated by EF Migrations.

ApplicationDbContextModelSnapshot.cs

20150929181902_Init.cs

20150929181902_Init.Designer.cs

(I noticed I forgot to include the Building > City relation when generating files for upload, but that shouldn't matter for the example)

SaphuA
  • 3,092
  • 3
  • 39
  • 58
  • This could be a bug in EF7. What doesyour `OnModelCreating` method look like? – natemcmaster Sep 29 '15 at 16:56
  • Is that a typo? .Include(c => c.AspNetUser). Are they in the same context? Do you have a DbSet for ApplicationUser ? – Steve Greene Sep 29 '15 at 17:45
  • @SteveGreene That was a typo, just cleared it up. They are in the same context. Users are in the DbSet - it's a property in IdentityDbContext. – SaphuA Sep 29 '15 at 18:12
  • @natemcmaster I'm also thinking it could be a bug. OnModelCreating is empty - I'm using what is created by migrations. I will upload the generated files now. – SaphuA Sep 29 '15 at 18:19
  • You mean reverse engineering, not migrations right? You will need at least one onmodelcreating statement to do the user relation since it doesn't follow convention. – Steve Greene Sep 29 '15 at 18:25
  • I do mean migrations. This is done using code first. `IdentityDbContext` does have an OnModelCreating but I'm not inheriting it or anything. – SaphuA Sep 29 '15 at 18:28

2 Answers2

0

I've finally figured it out.

ApplicationUser inherits from IdentityUser which inherits from IdentityUser<string>. The generic type is used as the primary key, so a string!

I changed my Guid foreign key properties to strings and now everything is working.

SaphuA
  • 3,092
  • 3
  • 39
  • 58
0

Just to help anyone who also have a similar issue. I was facing same thing, same scenario: My custom identity user has some related entities and using .Include was not working. None of them would come back. But they used to.

Turns out I had created a custom ApplicationUserStore to automatically include the entities I would normally always need when retrieving a User but I had only overridden FindByIdAsync method. When I tried to use FindByNameAsync no entity would come back, obviously. Just had to override this and the other Find methods.

Geter Moura
  • 65
  • 1
  • 8