Migrating from ASP.NET Core 1.x to ASP.NET Core 2.0 – Issues Continued

203 views 0 comments posted at about 3 months ago Raymond

Other related issues are found during my migration.

Unable to Change Identity Table Names

https://stackoverflow.com/questions/46118930/unable-to-change-asp-identity-table-names-asp-net-core-2

I faced the same issue as the above post. To fix it, I need to derive my database context with all the parameters specified:

public sealed class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int, IdentityUserClaim<int>, IdentityUserRole<int>, IdentityUserLogin<int>, ApplicationRoleClaim, IdentityUserToken<int>>

.Net Core 2.0 Web API OpenIddict Authorization: redirecting to login instead of returning JSON data

https://stackoverflow.com/questions/45784814/net-core-2-0-web-api-openiddict-authorization-redirecting-to-index-instead-of

Changed the following code in Startup.cs

// Register the OAuth2 validation handler as required by oidc
             services.AddAuthentication().AddOAuthValidation();

To:

// Register the OAuth2 validation handler as required by oidc
             services.AddAuthentication(options =>
             {
                 options.DefaultAuthenticateScheme = OAuthValidationDefaults.AuthenticationScheme;
             }).AddOAuthValidation();

The property 'Roles' is not a navigation property of entity type 'ApplicationUser'. The 'Include(string)' method can only be used with a '.' separated list of navigation property names. or

The property 'Users' is not a navigation property of entity type 'ApplicationRole'. The 'Include(string)' method can only be used with a '.' separated list of navigation property names.

Based on the suggestions from Microsoft Docs, I added the navigation propertied back as suggested but now I am getting these errors. In EntityFramework Core 2.0, Fluent API has not implemented many to many relationships. The example given in the office documentation will define role and user relationship as one to many and one extra field can be added to your database model. So eventually I have decided to use ApplicationUserRole to do the work.

Define a custom class first:

public class ApplicationUserRole : IdentityUserRole<int>
     {
         public virtual ApplicationRole Role { get; set; }

        public virtual ApplicationUser User { get; set; }
     }

In Application Role, remove the Users navigation property added previously with the following:

#region  asp.net core 2.0 support
         /// <summary>
         /// Navigation property for the claims this role possesses.
         /// </summary>
         public virtual ICollection<ApplicationRoleClaim> Claims { get; set; }

        public virtual ICollection<ApplicationUserRole> UserRoles { get; set; }
         #endregion

In Application User, remove the Roles navigation property added previously with the following:

#region  asp.net core 2.0 support
         /// <summary>
         /// Navigation property for the claims this user possesses.
         /// </summary>
         public virtual ICollection<IdentityUserClaim<int>> Claims { get; set; }
         /// <summary>
         /// Navigation property for this users login accounts.6
         /// </summary>
         public virtual ICollection<IdentityUserLogin<int>> Logins { get; set; }

        public virtual ICollection<ApplicationUserRole> UserRoles { get; set; }
         #endregion

In all my projects, I then changed to use UserRoles to find all the users in one role or vice versa. This is the only way I found it work to migrate.

After more than one day’s work, I have now finally migrated my asp.net core 1.1 web application to asp.net core 2.0 with OIDC server integrated as well.

Add comment

Comments (0)

No comments yet.
In this Page