1

I am trying to get grip on mvc4 , i just created a new Internet Application using asp.net mvc4 . I want to know that how to generate SimpleMemberShipProvider in asp.net mvc4 . They added a new class in Filter folder named SimpleMembershipInitializer but i don't know how to generate tables using that class .

Thanks in advance .

Ancient
  • 3,007
  • 14
  • 55
  • 104
  • I edited my answer to provide some extra background. If you are using the Internet Application template in MVC4 then it will create a localdb database in your App_Data folder the first time you run it, so you don't need to create your own db - the whole thing will be done for you. – Andy Brown May 30 '13 at 05:45
  • @AndyBrown yes you are right , it create a defaultconnection.mdf file but when i look into that database , there is only one table name `UserProfile` and there's only two fields in it named `UserId , UserName` , Where is the rest of membership Tables ? – Ancient May 30 '13 at 05:53
  • Have you registered a new user yet? – Andy Brown May 30 '13 at 06:06
  • Shit Man , Wasted my hour , now when tried to Register a user , i automatically creates everything Thanks alot . Thanks again – Ancient May 30 '13 at 06:16
  • I have now emphasised this in my answer. To save yourself more time later, I suggest looking at the references in my answer, and especially consider code-first and merging UserProfile into your `DbContext` – Andy Brown May 30 '13 at 06:24
  • @AndyBrown if i go through the procedure that hasbeen told in `code-first scenario` link , then i that case membership tables will be created or not ? – Ancient May 30 '13 at 06:33
  • Best so ask a new question on that one - it is OT for your original question. Short answer is yes (as long as you set it all up correctly) – Andy Brown May 30 '13 at 06:38

2 Answers2

3

If you have created your database, and set your connection string in web.config to that database, then when you first go to Account/Login it will generate the tables for you. Note that not all tables will be generated until you first create a user through "Register", then you will also see the tables starting with webpages_

EDIT If you are using the Internet Application template in MVC4 then it the default template is going to set you up with a localdb in the App_Data folder. This will be created the first time you run the application and call a database. (end edit)

Have a look at my answer here to "How do I use my own database with SimpleMembership and WebSecurity? What is MVC4 security all about?" to get an overview of SimpleMembership and how it all fits together.

Note that if you don't call Account/Login, then the database will not initialise, therefore in certain circumstances you will need to initialise it yourself, you can use the following lines in your Application_Start in global.asax.cs, or see my answer here to "ASP.NET MVC 4, The “WebSecurity.InitializeDatabaseConnection” method can be called only once" for more comprehensive scenarios.

if (!WebSecurity.Initialized)
{
    WebSecurity.InitializeDatabaseConnection("myConnectionStringName", "Users", "UserId", "Email", autoCreateTables: true);
}

Alternatively you can register InitializeSimpleMembership as a global filter, which would have the same effect as using the above code in global.asax.cs.

InitializeSimpleMembership simply does the above lines of code for you, but because it is only decorating the AccountController in the template provided, it will only kick in if you visit one of the Actions on that controller. This is where most of us trip up the first time. The MVC Internet Application template is only providing a sample way of using it. You need to actually ensure it is used across the entire site in order to have a production ready system.


Footnote:

Now that you've started using SimpleMembership it is worth knowing straight away that it works best in a code-first scenario, and that it is easiest to immediately put the UserProfile model class into your own DbContext that you are using for all the other Models in your code; this will prevent a lot of problems for you later. I will leave you to ask more questions on this separately as it is slightly off-topic.

Note that you can use the same connection string name for both your DbContext and the SimpleMembership (by default this is called "DefaultConnection" in the visual studio project template).

Community
  • 1
  • 1
Andy Brown
  • 18,961
  • 3
  • 52
  • 62
0

1) If you provide a default connection string in the web.config, the membership will create the database automacaly at the first login!

2) If you dont provide any connection, the app will look for the default connection in the machine.config to create the database at the SQL Instance or elsewhere mapped there.

3) If you need to create a custom MemberShipProvider take a look here: SimpleMembership, Membership Providers,

Hopes this help you!

Fals
  • 6,813
  • 4
  • 23
  • 43