Question

Today, I created a new ASP.NET MVC 4 project - Internet application.

With all the files that comes in the solution in the Account Controller I saw an attribute called [InitializeSimpleMembership]

[Authorize]
[InitializeSimpleMembership]
public class AccountController : Controller
{}

and by only providing a proper db connectionstring property in the web.config

<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.;
Initial Catalog=demo;
User ID=test;Password=test;" providerName="System.Data.SqlClient" />

and when the application runs, it checks if the available database exists or not and if not creates one and adds few memebership tables to it also. I must say I was very very impressed with the new MVC 4 - Internet app template.

Which brings me to my question : I liked the idea of creating database and executing script from the application. I wanted to know how to add more tables to this.

For example : In the InitializeSimpleMembershipAttribute.cs file it check if a database is present, if not it creates it using the credentials present in the web.config and also adds the following tables to it.

  • UserProfile
  • webpages_Membership
  • webpages_OAuthMembership
  • webpages_Roles
  • webpages_UsersInRoles

what changes do I have to make to have my other tables to be added along with this ?

If this is possible, the idea of keeping sql scripts and executing them on each clean deploy can be avoided. I am working on a self-host MVC app ( It's Open-source, soon to come on Codeplex ;) ) so this will really be good for people who want to use my app without getting their hands dirty in SQL scripts.

Please can some one let me know if this is even possible. Thanks

Was it helpful?

Solution

There are two distinct parts to the tables being created:

  1. The UserProfile table is created by adding the attribute [Table("UserProfile")] before the model in Models/AccountModels.cs (remember to add it as a DbSet to your context as well).
  2. The 'webpages_' tables are created by the WebSecurity.InitializeDatabaseConnection call in Filters/InitializeSimpleMembershipAttribute.cs called by adding the [InitializeSimpleMembership] in Controllers/AccountController.cs. These are 'internal' tables required by the SimpleMembershipProvider and you would not typically want to alter them.

This is called the CodeFirst method of the EntityFramework (your other options are ModelFirst and DbFirst). To have your own POCOs/models auto created you would want to add the Table("TableName") attribute similar to UserProfile above.

Refer to the Entity Framework Website for full walkthroughs of the CodeFirst approach (and much much more). http://msdn.microsoft.com/en-us/data/ef.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top