Month: August 2018

SQL skips id with auto increment

When using auto increment in the new SQL Server it sometimes leave gaps of serveral ids. I just encountered several thousands in gap. This is because of the SEQUENCE that was introduced in the 2012 version of SQL Server.

To turn off this feature and enable the +1 in increment without the gaps just follow these steps:

  • Run SQL Server Configuration Manager.
  • Select SQL Server Services

SQL Server Configuration Manager

  • Right-click SQL Server and select Properties.
  • In the opening window under Startup Parameters, type -T272 and click Add, then press Apply button and restart.

SQL Server startup parameters

This should fix it.

ASP.NET Identity 2.1 implementation for MySQL

For work purposes I needed to find a way to implement Asp.Net Membership provider into a MySQL database. Another requirement was that it needed to work with a MVC 5 Web Application.

After a few days of trying different things I came to the conclusion that MVC 5 is to much intergrated with .Net Identity.. So it was time to make the decision to go with Identity..

Easier said then done..

Gladly i came across a blog post of someone who faced the same issue:

In this blog post I will try to cover how to use a custom ASP.NET identity provider for MySQL I have created.

Default ASP.NET Identity provider uses Entity Framework and SQL Server to store information’s about users.

If you are trying to implement ASP.NET Identity 2.1 for MySQL database, then follow this guide.

This implementation uses Oracle fully-managed ADO.NET driver for MySQL.

This means that you have a connection string in your web.config similar to this:

<add name="DefaultConnection" connectionString="Server=localhost;
Database=aspnetidentity;Uid=radenko;Pwd=somepass;" providerName="MySql.Data.MySqlClient" />

This implementation of ASP.NET Identity 2.1 for MySQL has all the major interfaces implemented in custom UserStore class:

ASPIdentityUserStoreInterfaces

First, you will need to execute this a create script on your MySQL database which will create the tables required for the ASP.NET Identity provider.

MySqlAspIdentityDatabase

  • Create a new ASP.NET MVC 5 project, choosing the Individual User Accounts authentication type.
  • Uninstall all EntityFramework NuGet packages starting with Microsoft.AspNet.Identity.EntityFramework (Dont delete the Owin one)
  • Install NuGet Package called MySql.AspNet.Identity
  • In ~/Models/IdentityModels.cs:
    • Remove the namespaces:
      • Microsoft.AspNet.Identity.EntityFramework
      • System.Data.Entity
    • Add the namespace: MySql.AspNet.Identity.
      Class ApplicationUser will inherit from IdentityUser class in MySql.Asp.Net.Identity namespace
    • Remove the entire ApplicationDbContext class. This class is not needed anymore.
  • In ~/App_Start/Startup.Auth.cs
    • Delete this line of code:
app.CreatePerOwinContext(ApplicationDbContext.Create);
  • In ~/App_Start/IdentityConfig.cs
    Remove the namespaces:

    • Microsoft.AspNet.Identity.EntityFramework
    • System.Data.Entity
  • In method Create inside ApplicationUserManager class replace ApplicationUserManager with another which accepts MySqlUserStore :
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
    //var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
       var manager = new ApplicationUserManager(new MySqlUserStore<ApplicationUser>());

MySqlUserStore accepts an optional parameter in the constructor – connection string so if you are not using DefaultConnection as your connection string you can pass another connection string.

After this you should be able to build your ASP.NET MVC project and run it successfully using MySQL as a store for your user, roles, claims and other information’s.

Source: http://blog.developers.ba/asp-net-identity-2-1-for-mysql/