dotConnect for MySQL Documentation
In This Topic
    Using Entity Framework Implementation of ASP.NET Identity 2 for MySQL
    In This Topic

    ASP.NET Identity is a new system of user authentication and authorization, that continues the evolution of ASP.NET membership system, and is used in the Visual Studio 2013 project templates for ASP.NET MVC, Web Forms, Web API and SPA. You can read more on ASP.NET Identity in Microsoft documentation.

    dotConnect for MySQL enables you to employ an implementation of ASP.NET Identity for MySQL database using either ADO.NET or Entity Framework functionality in your web applications. This allows you to use such ASP.NET Identity benefits as unit-testable user authentication system, social login support, OWIN integration, etc. This tutorial demonstrates creating an Entity Framework implementation of ASP.NET Identity.

    To complete this tutorial you need Visual Studio 2013 Update 2 or higher installed.

    In order to create an ASP.NET MVC 5 application using dotConnect for MySQL for storing identity information, perform the following steps:

    1. In the Add New Project dialog box expand Visual C# on the left, then Web, and then select ASP.NET Web Application. Name your project, for example, "AspNet_Identity_Application" and then click OK.

      Add New Project dialog box
    2. In the New ASP.NET Project dialog box, select the MVC template with the default options (that includes Individual User Accounts as authentication method) and click OK.

      New ASP.NET Project dialog box

    3. Add references to the necessary dotConnect for MySQL assemblies:

      • Devart.Data.dll
      • Devart.Data.MySql.dll
      • Devart.Data.MySql.Entity.EF6.dll

      You need to add the last file from the Entity\EF6 subfolder of the dotConnect for MySQL installation folder. Or you may add it from the GAC.

    4. In the web.config file of your project, replace the default connection string with your MySQL one.

      <add name="DefaultConnection" connectionString="User Id=root;Host=localhost;Database=Test;" providerName="Devart.Data.MySql" />
      	
    5. Register the Entity Framework provider in the <providers> section of the web.config file

      Before the registration:

      	<providers>
      	  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      	</providers>
      	

      After the registration:

      	<providers>
      	  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      	  <provider invariantName="Devart.Data.MySql" type="Devart.Data.MySql.Entity.MySqlEntityProviderServices, Devart.Data.MySql.Entity.EF6, Version=8.3.215.0, Culture=neutral, PublicKeyToken=09af7300eec23701" />	
      	</providers>
      	

      Note: replace "8.3.215.0" with the actual version.

    6. Open the IdentityModels.cs file from the Models project folder.
    7. Add the following namespaces to the using list:

      using System.Data.Entity;
      using System.Data.Entity.Migrations;
      
      
      Imports System.Data.Entity
      Imports System.Data.Entity.Migrations
      
      
    8. Add the new Configuration class and register the dotConnect for MySQL provider SQL generator for Code-First Migrations.

      	internal class Configuration : DbMigrationsConfiguration<ApplicationDbContext>
      	{
      		public Configuration()
      		{
      			AutomaticMigrationsEnabled = true;
      			SetSqlGenerator("Devart.Data.MySql", new Devart.Data.MySql.Entity.Migrations.MySqlEntityMigrationSqlGenerator());
      		}
      	}
      
      
      	Friend Class Configuration
      		Inherits DbMigrationsConfiguration(Of ApplicationDbContext)
      		Public Sub New()
      			AutomaticMigrationsEnabled = True
      			SetSqlGenerator("Devart.Data.MySql", New Devart.Data.MySql.Entity.Migrations.MySqlEntityMigrationSqlGenerator())
      		End Sub
      	End Class
      
      
    9. Extend the ApplicationDbContext class code.

      We need to add a static constructor setting the database initializer for the context.

      After this we also need to add the OnModelCreating method, where we change type for character properties that aren't a part of entity key (they are filtered by their names) from the LONGTEXT data type to VARCHAR(256) via a lightweight convention.

      The code before editing:

      public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
        {
      		public ApplicationDbContext()
      			: base("DefaultConnection", throwIfV1Schema: false)
      	  {
      		}
      		public static ApplicationDbContext Create()
      	  {
      			return new ApplicationDbContext();
      		}
      	}
      
      
      Public Class ApplicationDbContext
      	Inherits IdentityDbContext(Of ApplicationUser)
      	Public Sub New()
      		MyBase.New("DefaultConnection", throwIfV1Schema:=False)
      	End Sub
      	Public Shared Function Create() As ApplicationDbContext
      		Return New ApplicationDbContext()
      	End Function
      End Class
      
      

      The code after editing:

      	public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
      		{
      		public ApplicationDbContext()
      			: base("DefaultConnection", throwIfV1Schema: false)
      		{
      		}
      		static ApplicationDbContext()
      	  {
      			Database.SetInitializer(new MigrateDatabaseToLatestVersion<ApplicationDbContext, Configuration>());
      		}
      		 protected override void OnModelCreating(DbModelBuilder modelBuilder)
      		{
      
      			base.OnModelCreating(modelBuilder);
      
      			modelBuilder
      				.Entity<IdentityRole>()
      				.Property(p => p.Name)
      				.HasMaxLength(255);
      
      			modelBuilder
      				.Entity<IdentityUser>()
      				.Property(p => p.UserName)
      				.HasMaxLength(256);
      
      			modelBuilder
      			  .Properties()
      			  .Where(p => p.PropertyType == typeof(string) &&
      						  !p.Name.Contains("Id") &&
      						  !p.Name.Contains("Provider"))
      			  .Configure(p => p.HasMaxLength(255));
      
      		}
      		public static ApplicationDbContext Create()
      		{
      			return new ApplicationDbContext();
      		}
      	}
      
      
      	Public Class ApplicationDbContext
      		Inherits IdentityDbContext(Of ApplicationUser)
      		Public Sub New()
      			MyBase.New("DefaultConnection", throwIfV1Schema:=False)
      		End Sub
      		Shared Sub New()
      			Database.SetInitializer(New MigrateDatabaseToLatestVersion(Of ApplicationDbContext, Configuration)())
      		End Sub
      		Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
      
      			MyBase.OnModelCreating(modelBuilder)
      
      			modelBuilder.Entity(Of IdentityRole)().[Property](Function(p) p.Name).HasMaxLength(255)
      			modelBuilder.Entity(Of IdentityUser)().[Property](Function(p) p.UserName).HasMaxLength(256)
      			modelBuilder.Properties().Where(Function(p) p.PropertyType = GetType(String) _
      				AndAlso Not p.Name.Contains("Id") AndAlso Not p.Name.Contains("Provider")).Configure(Function(p) _
      				p.HasMaxLength(255))
      			End Sub
      		Public Shared Function Create() As ApplicationDbContext
      			Return New ApplicationDbContext()
      		End Function
      	End Class
      
      

    Checking Application

    Now we can run our application and check if everything works correctly.

    1. Run the application by pressing CTRL+F5.

      Getting started page
    2. Switch to the Register tab on the top of the page.

      Registration page
    3. Enter a new Email and password to the corresponding boxes and click the Register button.
    4. The new user is now registered and logged in, and the ASP.NET Identity tables are created in the MySQL database.

      User is registered
    5. After this you may use an appropriate database tool to connect to the database where the user data is stored and verify that the data is stored correctly.

    See Also

    Using Entity Framework Implementation of ASP.NET Identity 1 for MySQL  | Using ADO.NET Implementation of ASP.NET Identity 1 for MySQL  | Using ADO.NET Implementation of ASP.NET Identity 2 for MySQL  | Using Entity Framework Core Implementation of ASP.NET Core Identity for MySQL