Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
98 views
in Technique[技术] by (71.8m points)

c# - ASP.Net Identity how to set target DB?

I am using the ASP.NET Identity Sample from the Asp-Team, and i am trying to change the database for the IdentityDbContext...

I tried it with the constructor

public MyDbContext() : base("MyConnectionString") { } // base is IdentityDbContext

like with a DbContext class.

That works well until i try to Register a User...

await IdentityStore.CreateLocalUser(user, model.Password)

returns false... no error, nothing.

Any ideas how to fix that?

Edit:

Filename is ModelsAppModel.cs, there is the MyDbContext class

originally it was

public class MyDbContext : IdentityDbContext<MyUser, UserClaim, UserSecret, UserLogin, Role, UserRole>
{
}

i changed it to

public class MyDbContext : IdentityDbContext<MyUser, UserClaim, UserSecret, UserLogin, Role, UserRole>
{
    public MyDbContext() : base("MyConnectionString") { }
}

the connection string is working because i have other projects using the same, and they run fine.

<connectionStrings>
    <add name="MySailorContext" connectionString="Data Source=THOMAS-LAPTOP;Initial Catalog=MySailor;Integrated Security=True;Pooling=False;MultipleActiveResultSets=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's a step-by-step on how to successfully change your database. First, clean-up anything you might have done previously. If there is any chance you might not get EVERYTHING, it would be best if you just started with a completely fresh copy in a new folder.

Once source is 100% back to original state, ensure everything else is cleaned up, including deleting your "new" database and the original database (aspnet-AspnetIdentitySample-20130627083537_2). You can find the original database using the SQL Server Object Explorer. ("View" -> "SQL Server Object Explorer" in Visual Studio)

Next, before you run your new application for the first time, go ahead and make the change to use your database name of choice. Here are the steps:


1. Set new database connection in web.config
  <connectionStrings>
    <!-- Original Connection String -->
    <!--
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)v11.0;
         Initial Catalog=aspnet-AspnetIdentitySample-20130627083537_2;Integrated Security=True"
      providerName="System.Data.SqlClient" />
    -->
    <!-- New Connection String -->
    <add name="MyConnectionString" connectionString="Data Source=(LocalDb)v11.0;
         Initial Catalog=MyAspnetIdentitySample_1;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

2. Modify AppModel.cs to have DBContext use new connection:

OLD:

    public class MyDbContext : IdentityDbContext<MyUser, UserClaim, UserSecret, UserLogin, Role, UserRole>
    {
    }

NEW:

    public class MyDbContext : IdentityDbContext<MyUser, UserClaim, UserSecret, UserLogin, Role, UserRole>
    {
        public MyDbContext() : base("MyConnectionString") { }
    }

3. Enable database migrations, create seed data and update to validate

3.1- In the package manager console, enable database migrations:

PM> enable-migrations

3.2 - Update MigrationsConfiguration.cs to enable automatic migrations and seed data

    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
    }

    protected override void Seed(AspnetIdentitySample.Models.MyDbContext context)
    {

        context.Users.AddOrUpdate(
            p => p.UserName,
            new MyUser { UserName = "John Doe" }
        );
    }

3.3 - Create the database through migration. In the Package Manager Console:

PM> update-database

3.4 - Validate that your new database was created (and that the originally provided db name doesn't exist) by using SQL Server Object Explorer and looking for the database "MyAspnetIdentitySample_1".

4. Run the app and create a new login to verify

This is how you can successfully do it from scratch -- without you providing more detail, I can't troubleshoot it with/for you. You should be able to determine where you went wrong.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...