Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change DefaultConnection in MVC Accounts Database

I'm new to Asp.Net MVC. My question is how to change DefaultConnection in the web.config file to use my entity framework connection string. my goal is one database in whole application.

I'm using Asp.Net MVC 5, and entity framework database first. the other posts I found here, doesn't specify my problem correctly.

Just tell me if I have to bring more information. Thanks.

like image 838
Kourosh Avatar asked Mar 02 '14 01:03

Kourosh


2 Answers

In you IdentityModels you should change the connectionString name:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }
}

Change the DefaultConnection to your testEntities.

like image 186
Jelle Oosterbosch Avatar answered Nov 05 '22 05:11

Jelle Oosterbosch


Find your AccountModels.cs/vb file and change

public class UsersContext : DbContext
{
    public UsersContext()
        : base("TestEntities")
    {
    }
...
}

I'm not sure if MVC 5 does the same initial file structure as MVC3- if so find in Filters folder InitializeSimpleMembershipAttribute.cs/vb file

private class SimpleMembershipInitializer
    {
        public SimpleMembershipInitializer()
        {
            Database.SetInitializer<UsersContext>(null);

            try
            {
                ...
//change this
                WebSecurity.InitializeDatabaseConnection("TestEntities", "UserProfile", "UserId", "UserName", autoCreateTables: true);
            }...
        }

If those files aren't present. Do Ctrl+F and search ~Entire Project for DefaultConnection and replace it with your desired connection string from Webconfig

like image 1
C.Corvax Avatar answered Nov 05 '22 06:11

C.Corvax