Last post Apr 29, 2015 06:15 AM by gtscdsi
Apr 26, 2015 11:57 PM|mlinsenbardt|LINK
I am working on an MVC 5 application and am using WebMatrix for my authentication. My Global.asax looks like this:
protected void Application_Start()
WebSecurity.InitializeDatabaseConnection("UsersContext", "UserProfile", "UserId", "UserName", true);
I have a SimpleInitializer for seeding data that looks like this:
protected override void Seed(UsersContext context)
var users = new List<UserProfile>
users.ForEach(s => context.UserProfiles.Add(s));
foreach (UserProfile user in users)
WebSecurity.CreateAccount(user.UserName, user.UserPassword, false);
catch (Exception e)
I have checked the membership tables after the calls to InitializeDatabaseConnection, and they are being created fine. In fact the registration and Login all work fine. The problem is when the Initializer is called, the membership tables are dropped and
I am getting 'Invalid object 'webpages_Membership' exception when I try to add the new users to the table.
Is there a way to prevent these membership tables from being dropped? I have been unable to find a solution to this problem anywhere and I would like to be able to have UserInitialization for this app.
Apr 29, 2015 06:15 AM|gtscdsi|LINK
Thanks for your post.
I found below information from:
http://www.codeproject.com/Tips/814618/Use-of-Database-SetInitializer-method-in-Code-Firs, please check the detail explanation for DropCreateDatabaseAlways<TContext> below, hope it can help you solving the problem.
1.CreateDatabaseIfNotExists<TContext>: This is the default option. For the first time when the application runs the Entity Framework Code First creates database if it does not exist. If your database already exists and you have done some modification to
the model and now you are running again the application, then you will get an exception of type InvalidOperationException.
2.DropCreateDatabaseAlways<TContext>: As the name suggests it always drops and recreates the database when the application runs first time. It deletes all the tables as the database is dropped.
3.DropCreateDatabaseIfModelChanges<TContext>: It drops and recreates the databases only when there is some changes or modifications in the model. Code First compare the version of in memeory model with that is stored in the _MigrationHistory table to know whether
the Model is changed or not.