{"action":"created","issue":{"url":"https://api.github.com/repos/aspnet/Identity/issues/724","labels_url":"https://api.github.com/repos/aspnet/Identity/issues/724/labels{/name}","comments_url":"https://api.github.com/repos/aspnet/Identity/issues/724/comments","events_url":"https://api.github.com/repos/aspnet/Identity/issues/724/events","html_url":"https://github.com/aspnet/Identity/issues/724","id":128956651,"number":724,"title":"UserManager.FindByName does not load custom properties of UserIdentity","user":{"login":"erlimar","id":3629320,"avatar_url":"https://avatars.githubusercontent.com/u/3629320?v=3","gravatar_id":"","url":"https://api.github.com/users/erlimar","html_url":"https://github.com/erlimar","followers_url":"https://api.github.com/users/erlimar/followers","following_url":"https://api.github.com/users/erlimar/following{/other_user}","gists_url":"https://api.github.com/users/erlimar/gists{/gist_id}","starred_url":"https://api.github.com/users/erlimar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/erlimar/subscriptions","organizations_url":"https://api.github.com/users/erlimar/orgs","repos_url":"https://api.github.com/users/erlimar/repos","events_url":"https://api.github.com/users/erlimar/events{/privacy}","received_events_url":"https://api.github.com/users/erlimar/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-01-26T22:03:17Z","updated_at":"2016-01-27T22:33:18Z","closed_at":null,"body":"Package versions:\r\n```json\r\n{\r\n    \"dependencies\": {\r\n        \"EntityFramework.Commands\": \"7.0.0-rc1-final\",\r\n        \"EntityFramework.SQLite\": \"7.0.0-rc1-final\",\r\n        \"EntityFramework.MicrosoftSqlServer\": \"7.0.0-rc1-final\",\r\n        \"Microsoft.AspNet.Authentication.Cookies\": \"1.0.0-rc1-final\",\r\n        \"Microsoft.AspNet.Authorization\": \"1.0.0-rc1-final\",\r\n        \"Microsoft.AspNet.Identity.EntityFramework\": \"3.0.0-rc1-final\",\r\n        \"Microsoft.AspNet.StaticFiles\": \"1.0.0-rc1-final\",\r\n        \"Microsoft.AspNet.Mvc\": \"6.0.0-rc1-final\",\r\n        \"Microsoft.AspNet.Mvc.TagHelpers\": \"6.0.0-rc1-final\",\r\n        \"Microsoft.AspNet.Hosting\": \"1.0.0-rc1-final\",\r\n        \"Microsoft.AspNet.Server.Kestrel\": \"1.0.0-rc1-final\",\r\n        \"Microsoft.AspNet.IISPlatformHandler\": \"1.0.0-rc1-final\",\r\n        \"Microsoft.AspNet.Diagnostics\": \"1.0.0-rc1-final\",\r\n        \"Microsoft.AspNet.Session\": \"1.0.0-rc1-final\",\r\n        \"Microsoft.Extensions.Configuration\": \"1.0.0-rc1-final\",\r\n        \"Microsoft.Extensions.Configuration.Json\": \"1.0.0-rc1-final\",\r\n        \"Microsoft.Extensions.Configuration.EnvironmentVariables\": \"1.0.0-rc1-final\",\r\n        \"Microsoft.Extensions.Configuration.UserSecrets\": \"1.0.0-rc1-final\",\r\n        \"Microsoft.Extensions.Logging\": \"1.0.0-rc1-final\",\r\n        \"Microsoft.Extensions.Logging.Console\": \"1.0.0-rc1-final\",\r\n        \"Microsoft.Extensions.Caching.Memory\": \"1.0.0-rc1-final\"\r\n    },\r\n    \"frameworks\": {\r\n        \"dnx451\": {},\r\n        \"dnxcore50\": {}\r\n    }\r\n}\r\n```\r\n\r\nMy Startup services configuration:\r\n```cs\r\npublic class Startup\r\n{\r\n    public void ConfigureServices(IServiceCollection services)\r\n    {\r\n        var ef = services.AddEntityFramework();\r\n        ef.AddSqlServer().AddDbContext<AuthContext>(options =>\r\n        {\r\n            options.UseSqlServer(dbConnectionString);\r\n        });\r\n\r\n        services.AddIdentity<User, IdentityRole>(options =>\r\n        { /* ... */ })\r\n        .AddEntityFrameworkStores<AuthContext>()\r\n        .AddDefaultTokenProviders();\r\n    }\r\n}\r\n```\r\n\r\nMy Context:\r\n```cs\r\npublic class AuthContext : IdentityDbContext<Model.User>\r\n{\r\n    public DbSet<UserProfile> UserProfiles { get; set; }\r\n\r\n    protected override void OnModelCreating(ModelBuilder builder)\r\n    {\r\n        base.OnModelCreating(builder);\r\n            \r\n        builder.Entity<UserProfile>(b => \r\n        {\r\n            b.HasKey(p => p.UserId);\r\n            b.HasOne(p => p.User)\r\n             .WithOne(u => u.Profile)\r\n             .HasForeignKey<UserProfile>(p => p.UserId);\r\n        });\r\n    }\r\n}\r\n```\r\n\r\nMy Model:\r\n```cs\r\npublic class User : IdentityUser\r\n{\r\n    public UserProfile Profile { get; set; }\r\n}\r\n\r\npublic class UserProfile\r\n{\r\n    public string FirstName { get; set; }\r\n    public string LastName { get; set; }\r\n\r\n    public string UserId { get; set; }\r\n    public User User { get; set; }\r\n}\r\n```\r\n\r\nI am adding a user as follows:\r\n```cs\r\npublic class Account : Controller\r\n{\r\n    public Account(UserManager<User> userManager, AuthContext authContext)\r\n    {\r\n        UserManager = userManager;\r\n        AuthContext = authContext;\r\n    }\r\n\r\n    private UserManager<User> UserManager { get; set; }\r\n    public AuthContext AuthContext { get; set; }\r\n\r\n    [HttpPost]\r\n    public async Task<IActionResult> SignUp(SignUpViewModel model)\r\n    {\r\n        if (ModelState.IsValid)\r\n        {\r\n            /* ... */\r\n            var user = new User\r\n            {\r\n                UserName = tempUserName,\r\n                Email = model.Email\r\n            };\r\n\r\n            user.Profile = new UserProfile\r\n            {\r\n                FirstName = model.FirstName,\r\n                LastName = model.LastName\r\n            };\r\n\r\n            var result = await UserManager.CreateAsync(user, model.Password);\r\n\r\n            if (result.Succeeded)\r\n            { /* ... */ }\r\n        }\r\n\r\n        return View(model);\r\n    }\r\n}\r\n```\r\n\r\nAt some point I need to recover data from a user, and do as follows:\r\n```cs\r\nvar user = await UserManager.FindByNameAsync(userName));\r\n\r\n/* ... */\r\n\r\nvar firstName = user.Profile.FirstName;\r\n```\r\n\r\nThen I get an error:\r\n```\r\nAn unhandled exception occurred while processing the request.\r\nNullReferenceException: ...\r\n```\r\n\r\nI realized that `user.Profile` is null.\r\n\r\nOut of curiosity, I tried to load the profile alone without even assign to any variable.\r\n\r\n```cs\r\nvar user = await UserManager.FindByNameAsync(userName));\r\n\r\n// HACK: ???\r\nAuthContext.UserProfiles.SingleOrDefault(p => p.UserId == user.Id);\r\n/* ... */\r\n\r\nvar firstName = user.Profile.FirstName;\r\n```\r\n\r\nAnd suddenly my variable previous `user` was the value of `user.Profile` filled out correctly.\r\n\r\n> **I noted that the data are entered correctly in the database.**\r\n\r\n### Because my call of `UserManager.FindByName` not bring my completely filled object first?"},"comment":{"url":"https://api.github.com/repos/aspnet/Identity/issues/comments/175895478","html_url":"https://github.com/aspnet/Identity/issues/724#issuecomment-175895478","issue_url":"https://api.github.com/repos/aspnet/Identity/issues/724","id":175895478,"user":{"login":"rustd","id":1132652,"avatar_url":"https://avatars.githubusercontent.com/u/1132652?v=3","gravatar_id":"","url":"https://api.github.com/users/rustd","html_url":"https://github.com/rustd","followers_url":"https://api.github.com/users/rustd/followers","following_url":"https://api.github.com/users/rustd/following{/other_user}","gists_url":"https://api.github.com/users/rustd/gists{/gist_id}","starred_url":"https://api.github.com/users/rustd/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rustd/subscriptions","organizations_url":"https://api.github.com/users/rustd/orgs","repos_url":"https://api.github.com/users/rustd/repos","events_url":"https://api.github.com/users/rustd/events{/privacy}","received_events_url":"https://api.github.com/users/rustd/received_events","type":"User","site_admin":false},"created_at":"2016-01-27T22:33:17Z","updated_at":"2016-01-27T22:33:17Z","body":"Accessing the child entities through the DbContext is fine. \r\nFor the future add a new extensibility point which loads the child entities. (include extra data)\r\n"}}