Click here to Skip to main content
15,851,387 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everyone,
im creating a website for school and im using entity framework code firs as Data Access.
i have seen videos where people where activating membership and role provider in entity framework by doing a small communication with it via its APIs,
problem is that im using entity framework and the mvc4 Internet template on visual studio and when im want to do update-datbase to get membership table on localdb attached to visual studio its says: "Could not find stored procedure 'dbo.aspnet_CheckSchemaVersion'."
and i dont want to configure it through aspnet_regsql.exe like other ppl where doing on video only using entity framework.
Thanks in advnce...
Pankaj Nikam 17-May-13 21:44pm    
Can you please post the video link so that we can actually see what the author of the video has achieved?

MVC4 doesn’t use the same tables and infrastructure of MemberShip that Asp.Net or Asp.NET MVC3 used. You can see that just by creating a new MVC4 projet. You will notice that inside the Model folder that you have a class “AccountModel.cs”. Also, inside the AccountController.cs you will see the use of WebSecurity that come from WebMatrix namespace.

The default configuration ain’t a problem until you want to remove the DbContext from the Model folder (which shouldn’t be there but inside the DAL layer). Also, it’s ain’t a problem until you you like to auto generate the table inside the database with some data into it. Creating a user is pretty easy with WebSecurity but it’s not when you try to seed the database with it. In fact, you will get error message saying that WebSecurity need to be initialized before use and if you initialize it, the database might not be created by Entity Framework and then it crashes.

The solution is to use the Package Manager Console and to generate the database with some commands line. This processes is called “Migration”.
Removing default code

The first step is to clean up some code that doesn’t have its place. The first code to delete is inside AccountModels.cs inside the Models folder. You will see at the top of the file a class called UsersContext. This need to be removed.

The second code that need to be deleted is the filter InitializeSimpleMembershipAttribute. This filter create the tables for MemberShip if not created. Since we do not want to create on the fly but when the application start (or with commands line) we can delete this filter. It’s a good idea because it uses the UsersContext that we just deleted. This file is located inside the Filters folder.

The third code to remove is the filter use that is above the AccountController class inside AccountController.cs file.

Of course, now you have to setup your DbContext to be able to do what UsersContext was doing. If you compile now, it won’t compile because we have deleted the UsersContext. But, 2 lines of code is required inside AccountController.cs to be able to compile. The first one is to create in your DbContext a methbod to get the user by name and one to create a new user. Here is an example that use a “Service layer” to access the repository that will use a DbContext later.

Configure Entity Framework Migration

The next step is to make sure the the web.config contain the right provider for the role and membership. Inside web.config, in the system.web section be sure that you have the code below. If not, add it.

<roleManager enabled="true" defaultProvider="SimpleRoleProvider">
        <add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData"/>
    <membership defaultProvider="SimpleMembershipProvider">
        <add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData"/>

This will specify that we are using the SimpleRoleProvider and the SimpleMembershipProvider. To be honest, I think with MVC4 it’s the default configuration so you do not required to do this but this website suggest to have it.

Once done, you need to build the Migration Configuration. This is automatically done if you are using the Package Manager Console. This tool can be open by going inside VIEW>Other Windows>Package Manager Console.

The next step is to use this command below that will force the creation of the configuration file. This will be required only once. Do not forget to use “EnableAutomaticMigration”. This will let you update the database later accordingly to changes you made to your model and entity configuration.

Enable-Migrations –EnableAutomaticMigrations -Force

Since we have removed the UsersContext we won’t have any conflict doing the command above. Otherwise, we would have need to specify which DbContext we want the migration to occur. Also, if you already had a database initializer (IDatabaseInitializer) like a “DropCreateDatabaseIfModelChanges”, this won’t work with the DbMigrationsConfiguration. In fact, you need to remove any initializer because we are switching to the Migration configuration. Nevertheless, this will give us additionnal control, more manual control but at least the possibility to generate the database automatically and also seed the database with Membership and custom tables.

The command creates a folder called Migrations with a Configuration.cs. Inside the Seed method, you will be able to create all data you want inside the database. Here is a small snippet of code that create a role, a user and some entities.

internal sealed class Configuration : DbMigrationsConfiguration<WorkoutPlanner.Database.DatabaseContext>
    public Configuration()
        AutomaticMigrationsEnabled = true;

    protected override void Seed(WorkoutPlanner.Database.DatabaseContext context)

        "UserName", autoCreateTables: true);

        if (!Roles.RoleExists("Administrator"))

        if (!WebSecurity.UserExists("123123"))
            WebSecurity.CreateUserAndAccount("123123", "123123");

        if (!((IList<string>)Roles.GetRolesForUser("123123")).Contains("Administrator"))
            Roles.AddUsersToRoles(new[] { "123123" }, new[] { "Administrator" });

        context.Set<UserProfile>().Add(new UserProfile { UserId = 1, UserName = "123123" });

        context.Set<Workout>().Add(new Workout { Id = 1, Name = "My First workout user1", StartTime = DateTime.Now.Add(TimeSpan.FromDays(-10)), Goal = "Increase body mass" });
        context.Set<Workout>().Add(new Workout { Id = 2, Name = "My Second workout user1", StartTime = DateTime.Now, Goal = "Increase chest muscle, lower fat around abs" });


The last step is to create table schema and insert those values from the seed configuration. This is done by this command :

update-database -verbose -force

The verbose will show you the SQL statement executed and the force will regenerate the table even if data already exist in those tables.


Any time you change your model or the configuration of your entities, you need to run update-database command. This will drop tables, create them again, create FK and insert seed data.
Share this answer
Jaydeep Jadav dude
when i create a new mvc4 project they are not added to my web.config
any way thank you offered very nice and integrated solution.
Share this answer
This is not an answer. Please delete this and if you want to reply to any answer, then click on "Have a Question or Comment" in that Answer Box itself.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900