Set ASP.NET Identity ConnectionString property in .NET 4.5.1

13,479

Solution 1

Release Candidate

Works with Microsoft.AspNet.Identity.EntityFramework 1.0.0-rc1

In the parameterless constructor for AccountController, change the line

IdentityManager = new AuthenticationIdentityManager(new IdentityStore());

to

IdentityManager = new AuthenticationIdentityManager(new IdentityStore(new DefaultIdentityDbContext("YourNameOrConnectionString")));

and you're good to go.

Release

Works with Microsoft.AspNet.Identity.EntityFramework 1.0.0

Similar to what we did for release candidate, but we do this in a different place. Open IdentityModels.cs that was created as part of the VS template and add the following constructor to the ApplicationDbContext class:

public ApplicationDbContext(string nameOrConnectionString)
    : base(nameOrConnectionString)
{
}

and you can now change the parameterless constructor in AccountController from

public AccountController()
    : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
{
}

to

public AccountController()
    : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext("YourNameOrConnectionString"))))
{
}

and your done.

Solution 2

I have followed the approach you suggested and it worked for me. However, there were few things mostly syntactic and naming issues which happened to be different. I think these differences are due to probably the different versions of Visual Studios we used (rather than .NET - my version is release one with .NET 4.5.1). I continue with a description of my specific solution.

My goal was to have a single DB context with which I can access both User or Identity related data and also my custom application data. To achieve this I completely deleted class ApplicationDbContext which is automatically created for you when you create a new project.

Then, I created a new class MyDbContext.

public class MyDbContext: DbContext
{
    public MyDbContext() : base("name=DefaultConnection")
    {

    }

    //
    // These are required for the integrated user membership.
    //
    public virtual DbSet<IdentityRole> Roles { get; set; }
    public virtual DbSet<ApplicationUser> Users { get; set; }
    public virtual DbSet<IdentityUserClaim> UserClaims { get; set; }
    public virtual DbSet<IdentityUserLogin> UserLogins { get; set; }
    public virtual DbSet<IdentityUserRole> UserRoles { get; set; }

    public DbSet<Movie> Movies { get; set; }
    public DbSet<Order> Orders { get; set; }
    public DbSet<Purchase> Purchases { get; set; }
}

The fields Roles, Users, UserClaims, UserLogins, UserRoles are as suggested required for the membership management. However, in my case their types have different names (ApplicationUser instead of User, IdentityUserClaim instead of UserClaim and etc.). I suppose that was the reason why Antevirus had the "User could not be found" problem.

Also, as we see in my case there are 5 such fields rather than 8. Probably, this is due to the different versions of the Visual Studio.

The last change which I made was in class AccountController and it reflects the use of the new context MyDbContext. Here I passed an instance of MyDbContext instead of ApplicationDbContext

Before

public AccountController()
    : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
{
}

After

public AccountController()
    : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new MyDbContext())))
{
}
Share:
13,479
joelmdev
Author by

joelmdev

Career Microsoft stack developer with more than 15 years of experience in .NET and related technologies. Passionate software architect and Domain Driven Design aficionado. Founder and CEO of Tusk Software where I'm fortunate enough to work with a team as dedicated to our clients' success as I am (and we're hiring!).

Updated on July 29, 2022

Comments

  • joelmdev
    joelmdev over 1 year

    So basically after finally learning how to change OpenAuth to not use DefaultConnection in .NET 4.5, I've moved on to 4.5.1, rendering those learnings moot. The duties of AuthConfig.cs now reside in Startup.Auth.cs, The static methods of OpenAuth have been abstracted away and therefore I can no longer change the default value of OpenAuth.ConnectionString directly.

    What is the best practice for changing Membership's connection string/database in .NET 4.5.1?

  • Antevirus
    Antevirus over 10 years
    Hello. I added these to my own context, but i am getting "The type or namespace name 'User' could not be found (are you missing a using directive or an assembly reference?)". The same goes for all of the DbSet's added. Microsoft.AspNet.Identity.EntityFramework is added as a reference and as a using-directive. Any ideas?
  • joelmdev
    joelmdev about 10 years
    @Antevirus have a look at the new code I provided. You are using the release version of Microsoft.AspNet.Identity.EntityFramework which is why you're getting the error; User was renamed IdentityUser in the release version of the library. There were significant changes to the AccountController and the classes upon which it depends from 1.0.0-rc1 to 1.0.0. The new code I've provided does leave you with separate dbcontexts for you account models and domain models (and it's hard to keep them from overlapping sometimes) but it is much simpler and safer.
  • joelmdev
    joelmdev about 10 years
    Have a look at my edited answer. I believe it's much easier to use, and much safer. My previous answer did combine the two DbContexts into one, but it left out some code from the IdentityDbContext that probably shouldn't have been removed.