我怎样才能改变默认的数据库进行简单的会员表 [英] How can i change the default database for simple membership tables

查看:119
本文介绍了我怎样才能改变默认的数据库进行简单的会员表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下这个简单的问题关于附带asp.net的MVC 4.5的互联网应用程序的默认简单会员数据库,我的问题是关于它在哪里创建其表时,我第一次创建新的用户不会创建其表默认的连接字符串(的LocalDB)?如果我更改了默认的连接字符串使用自定义的MS SQL防爆preSS数据库将是那些获得反射,这些表将在我的新数据库中创建的?

我注意到,在默认帐户模型,并在其的DbContext类此code是指默认连接这是code有:

 公共类UsersContext:的DbContext
{
    公共UsersContext():基地(DefaultConnection)
    {
    }    公共DbSet<用户配置> {的UserProfiles获得;组; }
}


解决方案

SimpleMembership被初始化在 InitializeSimpleMemberhsipAttribute.cs 类。

在默认的MVC 4互联网项目模板,导航到过滤器目录,并在上面的类,你会发现初始化方法:

  WebSecurity.InitializeDatabaseConnection(DefaultConnection,用户配置,
 用户ID,用户名,autoCreateTables:真正的);

这是默认设置,如果你想在不同的数据库来点做这样的事情:

  WebSecurity.InitializeDatabaseConnection(MyContext,TableToPointTo
UserIdColumn,UserNameColumn,autoCreateTables:假);

在你的上下文类:

 公共类MyContext:的DbContext {...}

结构>在根的web.config 部分和里面的&LT的连接字符串部分更改名称>文件:

 <&是connectionStrings GT;
    <添加名称=MyContext的connectionString =.../>
< /&是connectionStrings GT;

I would like to ask this quick question about the default simple membership database that comes with asp.net mvc 4.5 internet application , my question is about where does it create its tables when I first create new user does it create its table in the default connection string (LocalDB) ? and what if I changed the default connection string to use a custom MS SQL Express database will that get reflected and those tables will be created in my new database ?

i noticed this code in the default account model and in its DbContext class it refers to default connection this was the code there :

public class UsersContext : DbContext
{
    public UsersContext() : base("DefaultConnection")
    {
    }

    public DbSet<UserProfile> UserProfiles { get; set; }
}

解决方案

SimpleMembership gets initialised in the InitializeSimpleMemberhsipAttribute.cs class.

In the default MVC 4 internet project template, navigate to the Filters directory, and in the above class you'll find the initialising method:

WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile",
 "UserId", "UserName", autoCreateTables: true);

That's the default setup, if you want to point it at a different database do something like this:

WebSecurity.InitializeDatabaseConnection("MyContext", "TableToPointTo", 
"UserIdColumn", "UserNameColumn", autoCreateTables: false);

In your context class:

public class MyContext : DbContext {...}

And change the name in the connection strings section inside the <configuration> section in the root web.config file:

<connectionStrings>
    <add name="MyContext" connectionString="..." />
</connectionStrings>

这篇关于我怎样才能改变默认的数据库进行简单的会员表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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