The property 'Claims' on type 'AspNetUser' is not a navigation property
Asked Answered
A

1

13

I'm using ASP.NET Identity 2.2. I'm migrating the ASP.NET old membership to new Identity system. I am following the steps mentioned in this article for performing the migration.

I have extended IdentityUser and added few more properties like follows:

public partial class AspNetUser : IdentityUser
{
        public AspNetUser()
        {
            CreateDate = DateTime.Now;
            IsApproved = false;
            LastLoginDate = DateTime.Now;
            LastActivityDate = DateTime.Now;
            LastPasswordChangedDate = DateTime.Now;
            LastLockoutDate = DateTime.Parse("1/1/1754");
            FailedPasswordAnswerAttemptWindowStart = DateTime.Parse("1/1/1754");
            FailedPasswordAttemptWindowStart = DateTime.Parse("1/1/1754");
            Discriminator = "AspNetUser";
            LastModified = DateTime.Now;

            this.AspNetUserClaims = new HashSet<AspNetUserClaim>();
            this.AspNetUserLogins = new HashSet<AspNetUserLogin>();
            this.AspNetRoles = new HashSet<AspNetRole>();
        }
        ....
        public virtual Application Application { get; set; }
        public virtual ICollection<AspNetUserClaim> AspNetUserClaims { get; set; }
        public virtual ICollection<AspNetUserLogin> AspNetUserLogins { get; set; }
        public virtual ICollection<AspNetRole> AspNetRoles { get; set; }

}

There are few more properties in the AspNetUser class which is not included for brevity.

I am able to register the user successfully using the identity system:

 var manager = new ApplicationUserManager();
 var user = new AspNetUser
                {
                    UserName = UserName.Text.Trim(),
                    Email = Email.Text.Trim()
                };

 var result = manager.Create(user, Password.Text);

But when I'm searching for any user by email address/username then I'm getting an exception:

var existingUser = manager.FindByEmail(emailAddress);

The error is:

The property 'Claims' on type 'AspNetUser' is not a navigation property. 
The Reference and Collection methods can only be used with navigation properties. Use the Property or ComplexProperty method.

Update:

If I'm removing the AspNetUserClaims property from the AspNetUser class then I'm getting a list of new errors:

Schema specified is not valid. Errors: 
The relationship 'JanEntities.FK__AspNetU__Appli__628FA481' was not loaded because the type 'MyEntities.AspNetUser' is not available.
The following information may be useful in resolving the previous error:
The required property 'AspNetUserClaims' does not exist on the type 'SampleApp.Core.AspNetUser'.


The relationship 'MyEntities.AspNetUserRole' was not loaded because the type 'MyEntities.AspNetUser' is not available.
The following information may be useful in resolving the previous error:
The required property 'AspNetUserClaims' does not exist on the type 'SampleApp.Core.AspNetUser'.


The relationship 'MyEntities.FK_dbo_AspNetUserClaim_dbo_AspNetUser_User_Id' was not loaded because the type 'MyEntities.AspNetUser' is not available.
The following information may be useful in resolving the previous error:
The required property 'AspNetUserClaims' does not exist on the type 'SampleApp.Core.AspNetUser'.


The relationship 'MyEntities.FK_dbo_AspNetUserLogin_dbo_AspNetUser_UserId' was not loaded because the type 'MyEntities.AspNetUser' is not available.
The following information may be useful in resolving the previous error:
The required property 'AspNetUserClaims' does not exist on the type 'SampleApp.Core.AspNetUser'.

The following is the database diagram which contains the new ASP.NET identity tables: enter image description here

Can anyone help me to fix this issue? Any help is highly appreciated.

Ashanti answered 28/4, 2015 at 9:26 Comment(2)
Why do you need the AspNetUserClaims property in your model when IdentityUser already has a Claims property?Muzzle
@Muzzle : Thanks for your quick reply. I have added the new list of errors to the question. I'm getting those new errors if I'm removing AspNetUserClaims property from IdentityUserAshanti
S
0

You can check here properties of IdentityUser: https://msdn.microsoft.com/en-us/library/microsoft.aspnet.identity.entityframework.identityuser_properties(v=vs.108).aspx

As you can see, properties like Claims, Logins, Roles are already there. By default asp.net identity is using DbContext, which inherits from IdentityDbContext https://msdn.microsoft.com/en-us/library/microsoft.aspnet.identity.entityframework.identitydbcontext%28v=vs.108%29.aspx

This class configure many things like table mappings etc. Can we see your DbContext?

So first, try removing added ICollections and theirs initializers from constructor.

Sporulate answered 19/5, 2015 at 16:51 Comment(1)
removing virtual property would generate an error as he described abovePolygnotus

© 2022 - 2024 — McMap. All rights reserved.