ASP.Net身份如何设定目标DB? [英] ASP.Net Identity how to set target DB?

查看:117
本文介绍了ASP.Net身份如何设定目标DB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用从ASP-队 ASP.NET身份样本,我试图改变数据库在 IdentityDbContext ...

我用构造试过

 公共MyDbContext():基地(MyConnectionString){} //基地IdentityDbContext

像一个的DbContext类。

这是直到我尝试注册一个用户运行良好...

 等待IdentityStore.CreateLocalUser(用户,model.Password)

收益 ...没有错误,什么都没有。

任何想法如何解决呢?

编辑:

文件名是型号\\ AppModel.cs ,有 MyDbContext类

原来这是

 公共类MyDbContext:IdentityDbContext< MYUSER,UserClaim,UserSecret,用户登陆,角色的UserRole>
{
}

我改成了

 公共类MyDbContext:IdentityDbContext< MYUSER,UserClaim,UserSecret,用户登陆,角色的UserRole>
{
    公共MyDbContext():基地(MyConnectionString){}
}

连接字符串工作,因为我一直在使用相同的其他项目,它们运行正常。

 <&是connectionStrings GT;
    <添加名称=MySailorContext的connectionString =数据源= THOMAS-LAPTOP;初始目录= MySailor;集成安全性= TRUE;池= FALSE; MultipleActiveResultSets =真的providerName =System.Data.SqlClient的/>
< /&是connectionStrings GT;


解决方案

下面是关于如何成功地改变你的数据库一步一步的。首先,清理东西你可能已经pviously做$ P $。如果你可能无法得到任何一切机会,如果你刚开始在一个新的文件夹一个完全全新副本这将是最好的。

在源是100%回原来的状态,确保一切被清理,其中包括删除您的新的数据库和原始数据库(ASPNET-AspnetIdentitySample-20130627083537_2)。您可以使用SQL Server对象资源管理器的原始数据库。 (查看 - >在Visual Studio中的SQL Server对象资源管理器)

接下来,运行的第一次新的应用程序之前,继续前进,使使用你选择的数据库名称的变化。具体操作步骤如下:



1。设置在web.config中新的数据库连接

 <&是connectionStrings GT;
    <! - 原始连接字符串 - >
    <! -
    <添加名称=DefaultConnection的connectionString =数据源=(的LocalDB)\\ V11.0;
         初始目录= ASPNET-AspnetIdentitySample-20130627083537_2;集成安全性=真
      的providerName =System.Data.SqlClient的/>
     - >
    <! - 新的连接字符串 - >
    <添加名称=MyConnectionString的connectionString =数据源=(的LocalDB)\\ V11.0;
         初始目录= MyAspnetIdentitySample_1;集成安全性=真
      的providerName =System.Data.SqlClient的/>
  < /&是connectionStrings GT;

2。修改AppModel.cs拥有的DbContext使用新的连接:

OLD:

 公共类MyDbContext:IdentityDbContext< MYUSER,UserClaim,UserSecret,用户登陆,角色的UserRole>
    {
    }

新:

 公共类MyDbContext:IdentityDbContext< MYUSER,UserClaim,UserSecret,用户登陆,角色的UserRole>
    {
        公共MyDbContext():基地(MyConnectionString){}
    }

3。启用数据库迁移,创建种子数据和更新,以验证

3.1-在包管理器控制台,使数据库迁移:

  PM>允许的迁移

3.2 - 更新迁移\\ Configuration.cs启用自动迁移和种子数据

 公共配置()
    {
        AutomaticMigrationsEnabled = TRUE;
    }    保护覆盖无效的种子(AspnetIdentitySample.Models.MyDbContext上下文)
    {        context.Users.AddOrUpdate(
            P => p.UserName,
            新MYUSER {用户名=李四}
        );
    }

3.3 - 通过移民创建数据库。在Package Manager控制台:

  PM>更新数据库

3.4 - 验证您的新数据库使用SQL Server对象资源管理器,寻找数据库MyAspnetIdentitySample_1创建(即原来提供的数据库名称不存在)。

4。运行应用程序并创建一个新的登录验证

这是你怎么能成功地做到这一点从头开始 - 没有你提供更多的细节,我不能与/为你解决它。你应该能够确定你在哪里错了。

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 Models\AppModel.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>

解决方案

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 Migrations\Configuration.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.

这篇关于ASP.Net身份如何设定目标DB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆