Keep getting A specified Include path is not valid for Roles, Logins, and Claims

Apr 14, 2014 at 3:03 PM
Edited Apr 14, 2014 at 4:37 PM
Hi,

I've been using the ASP.NET Identity 1.0, with DatabaseFirst approach. The tables are ApplicationRole, ApplicationUserClaim, ApplicationUserLogin, ApplicationUser and ApplicationUserRole, and it worked.

Since i want to use the ResetPassword and EmailConfirmation, i migrated to Asp.NET Identity 2.0, and modify the database manually by following the example by pranav updating-asp-net-applications-from-asp-net-identity-1-0-to-2-0-0-alpha1, i didn't use the Enable-Migrations. Since from what i understand, it's just adding and deleting some columns.

And then i do an "Update model from database", and it compiled nicely. But the problem is when i login, i keep getting an error from the UserManager.FindAsync(model.Username, model.Password) : A specified Include path is not valid, the ApplicationUser does not declare a navigation property "Roles".

I tried modifying my table name from ApplicationRole to Role, and it passed, but then it got another error for A specified Include path is not valid, the ApplicationUser does not declare a navigation property "Claims".

I changed my table name from ApplicationUserClaim to Claim, and it also happens to Logins, so i changed from ApplicationUserLogin to Login.

Then the error change to Ambiguous Path, since i don't know which one, i reverted all the table names to the start.

How do i solve this error ? is there any chance not to load Roles, Logins, and Claims when using Find method ? Or do i miss something here ?

And i got no problem at all using the code below, it returns all users with all roles.
var users =  Db.ApplicationUsers.Include(c => c.ApplicationRoles);
Any help would be greatly appreciated

Thanks in advance



Stack trace below :
[InvalidOperationException: Un chemin d'accès Include spécifié n'est pas valide. L'EntityType 'Models.ApplicationUser' ne déclare pas de propriété de navigation avec le nom 'Roles'.]
   System.Data.Entity.Core.Objects.Internal.ObjectFullSpanRewriter.ConvertSpanPath(SpanPathInfo parentInfo, List`1 navPropNames, Int32 pos) +190
   System.Data.Entity.Core.Objects.Internal.ObjectFullSpanRewriter.AddSpanPath(SpanPathInfo parentInfo, List`1 navPropNames) +42
   System.Data.Entity.Core.Objects.Internal.ObjectFullSpanRewriter..ctor(DbCommandTree tree, DbExpression toRewrite, Span span, AliasGenerator aliasGenerator) +308
   System.Data.Entity.Core.Objects.Internal.ObjectSpanRewriter.TryRewrite(DbQueryCommandTree tree, Span span, MergeOption mergeOption, AliasGenerator aliasGenerator, DbExpression& newQuery, SpanIndex& spanInfo) +184
   System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlanFactory.Prepare(ObjectContext context, DbQueryCommandTree tree, Type elementType, MergeOption mergeOption, Boolean streaming, Span span, IEnumerable`1 compiledQueryParameters, AliasGenerator aliasGenerator) +134
   System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption) +2424
   System.Data.Entity.Core.Objects.<>c__DisplayClassc.<GetResultsAsync>b__a() +83
   System.Data.Entity.Core.Objects.<ExecuteInTransactionAsync>d__3d`1.MoveNext() +1533
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   System.Runtime.CompilerServices.ConfiguredTaskAwaiter.GetResult() +24
   System.Data.Entity.SqlServer.<ExecuteAsyncImplementation>d__9`1.MoveNext() +628
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   System.Runtime.CompilerServices.ConfiguredTaskAwaiter.GetResult() +24
   System.Data.Entity.Core.Objects.<GetResultsAsync>d__e.MoveNext() +1138
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   System.Runtime.CompilerServices.ConfiguredTaskAwaiter.GetResult() +24
   System.Data.Entity.Internal.<FirstMoveNextAsync>d__0.MoveNext() +544
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   System.Runtime.CompilerServices.ConfiguredTaskAwaiter.GetResult() +24
   System.Data.Entity.Infrastructure.<FirstOrDefaultAsync>d__25`1.MoveNext() +478
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   System.Runtime.CompilerServices.ConfiguredTaskAwaiter.GetResult() +24
   Microsoft.AspNet.Identity.<FindAsync>d__12.MoveNext() +543
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +24
   Microsoft.AspNet.Identity.AsyncHelper.RunSync(Func`1 func) +228
   Microsoft.AspNet.Identity.UserManagerExtensions.Find(UserManager`2 manager, String userName, String password) +330
   YYYY.Controllers.<Login>d__f.MoveNext() in c:\Users\xxxxxxxxxxx\Documents\Visual Studio 2013\Projects\YYYY\YYYY\Controllers\AccountController.cs:76
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   System.Runtime.CompilerServices.TaskAwaiter.GetResult() +21
   System.Threading.Tasks.TaskHelpersExtensions.ThrowIfFaulted(Task task) +61
   System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +114
   System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeAsynchronousActionMethod>b__36(IAsyncResult asyncResult) +66
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +47
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +136
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +49
   System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +117
   System.Web.Mvc.Async.<>c__DisplayClass48.<InvokeActionMethodFilterAsynchronouslyRecursive>b__41() +323
   System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +44
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +47
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +136
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +50
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +72
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +185
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44
   System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +39
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +62
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +39
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9514928
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
Developer
Apr 14, 2014 at 11:16 PM
Have you mapped the relationships between users and the other entities correctly in OnModelCreating. If the relationship is established correctly, then loading them shouldn't pose a problem.
Apr 15, 2014 at 1:32 PM
Edited Apr 15, 2014 at 1:39 PM
When i'm using ASP.NET identity 1.0, i didn't need to map entities OnModelCreating, since i used the Database First approach.

And just like i mentioned above, when i do the Db.ApplicationUsers, it loaded without any problem the ApplicationRoles. But only when using the FindById, it tried to load Roles, Claims and Logins, instead of ApplicationRoles, ApplicationLogins, and ApplicationClaims

Here is my classes, updated directly from database :
public partial class ApplicationRole
    {
        public ApplicationRole()
        {
            this.ApplicationUsers = new HashSet<ApplicationUser>();
        }
    
        public string Id { get; set; }
        public string Name { get; set; }
    
        public virtual ICollection<ApplicationUser> ApplicationUsers { get; set; }
    }

public partial class ApplicationUser
    {
        public ApplicationUser()
        {
            this.ApplicationUserClaims = new HashSet<ApplicationUserClaim>();
            this.ApplicationUserLogins = new HashSet<ApplicationUserLogin>();
            this.ApplicationRoles = new HashSet<ApplicationRole>();
        }
    
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public bool IsConfirmed { get; set; }
        public string ConfirmationToken { get; set; }
        public string Discriminator { get; set; }
    
        public virtual ICollection<ApplicationUserClaim> ApplicationUserClaims { get; set; }
        public virtual ICollection<ApplicationUserLogin> ApplicationUserLogins { get; set; }
        public virtual ICollection<ApplicationRole> ApplicationRoles { get; set; }
    }

public partial class ApplicationUserClaim
    {
        public int Id { get; set; }
        public string ClaimType { get; set; }
        public string ClaimValue { get; set; }
        public string UserId { get; set; }
    
        public virtual ApplicationUser ApplicationUser { get; set; }
    }

 public partial class ApplicationUserLogin
    {
        public string UserId { get; set; }
        public string LoginProvider { get; set; }
        public string ProviderKey { get; set; }
        public string User_Id { get; set; }
    
        public virtual ApplicationUser ApplicationUser { get; set; }
    }

    public partial class ApplicationRole : IdentityRole
    {
        public ApplicationRole(string name)
            : base(name)
        { }
    }

    public partial class ApplicationUser : IdentityUser
    {
        
    }
Thanks for helping me out
Apr 15, 2014 at 1:59 PM
I confirmed, i reverted back to Asp.NET Identity 1.0, but keep the EntityFramework 6.1, FindById method works again.
Developer
Apr 15, 2014 at 5:19 PM
I see that the UserLogin and UserClaim do not extend the IdentityUserLogin and IdentityUserClaim classes. Is that so ? They would definitely need to extend that. The relationships between the child entities was updated in the OnModelCreating for 2.0 to support generic Primary Key types. You might want to re check the relationships and how the Identity classes are mapped to update your database schema
Apr 16, 2014 at 10:49 AM
This is so frustrating. I can't make it work.

What i did until now trying to make it work (i'm using Database First)
  1. I created a new project, and run the Install-Package Microsoft.AspNet.Identity.Samples –Pre
  2. Launch the IdentitySample, and register a user.
  3. Making sure that the ASP.NET Identity tables in my production having all the columns like the tables in IdentitySample.
  4. Copying the IdentityConfig.cs and ApplicationUserManager from IdentitySample to my solution
I really have no more ideas how to make this work, same error, is the name of tables have some importance ?

Image