15

I have a database called ApplicationName_Development running on SQL Server 2008 R2 Developer edition on my development box.

I added .NET membership tables to the database with no problem. When I tried to get Code First working I received the following error message:

The server encountered an error processing the request. The exception message is "Model compatibility cannot be checked because the database does not contain model metadata. Ensure that IncludeMetadataConvention has been added to the DbModelBuilder conventions.

After some googling, I discovered that I had to delete the database and let EF create the database. That's fine but I lost all my .NET membership tables. I can go back in and add the membership tables again but if my model changes and EF needs to recreate the database then I have to add the membership tables in again.

How do I get around this?

6 Answers 6

6

This is how code-first work. Main idea of code first is that you do not touch your database because it is responsibility of the model to create the database. If you want to customize your database you must create custom IDatabaseInitializer and add your custom SQL.

public class MyDbInitializer : DropCreateDatabaseIfModelChanges<MyContext>
{
    protected override void Seed(MyContext context)
    {
        // Here run your custom SQL commands
        context.Database.ExecuteSqlCommand("CREATE TABLE ....");
    }
}

Now you only need setup your cutom intializer on the startup of your application:

Database.SetInitializer<MyContext>(new MyDbInitializer());

If you don't want to do it this way you must manually maintain your database and set initializer to null.

5
  • I get that but how does it work with the Membership Provider? I can add the Membership Provider to the database once it has been created but if I make a model change and need to recreate the database then it wipes out all the Membership Provider tables and it seems like I have to recreate them. I'm looking to not recreate the Membership Provide tables everytime I need to recreate the database.
    – Mike
    Mar 28, 2011 at 18:30
  • 1
    Then you are looking for database first instead of code first. Current version of EF doesn't have database evolution features. It can only drop database and create it from scratch. You can writy your own database initializer which will be able to only modify DB but it can be complicated like separate project. Mar 28, 2011 at 19:04
  • Check these related questions: stackoverflow.com/questions/4485742/… and stackoverflow.com/questions/5463121/… Mar 28, 2011 at 19:34
  • Thanks for the information. I guess until they implement migrations, we are out of luck.
    – Mike
    Mar 29, 2011 at 13:55
  • FYI (for people coming to this now), migrations support now exists (and it's pretty great, actually) Apr 5, 2012 at 17:22
5

Found a easier workaround here. I hope this helps.

http://www.paragm.com/ef-v4-1-code-first-and-asp-net-membership-service/

1
  • 2
    That is a very clever work around, but do you really want to recreate the membership functionality in every database you create from now on? Nov 5, 2011 at 15:56
5

Another option could be to use the System.Web.Management namespace. I've had great success with the code below:

string connectionString = ConfigurationManager.ConnectionStrings["MyDatabaseContext"].ConnectionString;
string database = "MyDatabaseName";
SqlServices.Install(database, SqlFeatures.All, connectionString);

It will just create the database and after that you can add users with the standard membership API.

3

Here's another possibility.

If you look at the MvcMusicStore sample - there's a SampleData class that is responsible for seeding the database on a rebuild. The SampleData class inherits from DropCreateDatabaseIfModelChanges, and overrides the Seed method. This class is passed to the Database.SetInitializer in the Application_Start method in global.asax.

I was getting the same error as you until I changed the parent class of SampleData to CreateDatabaseIfNotExist.

Then you can override the Seed method to insert any data you desire at startup, without it blowing away the database.

1
  • I've done the same but i still get a similar error. Instead of "Invalid object name dbo.Genres" i now get "Invalid object name dbo.Genre"...
    – Sys
    Jun 1, 2011 at 22:44
2

While you are developing, create 2 databases and two connection strings. One for SqlMembership (using aspnet_regsql) and one for your EF Application. If you would like to merge them into a single DB in production, just change the connection string in web.config.release to be the same. Then, EF model changes will just drop your apps db and not your membership DB.

By treating your authentication component separately, you will naturally decouple your authentication system from your application system. Then, if you wish to change membership providers, you will be better setup.

As the system grows, you will likely need to support non-pure models without EF code first, so this is a good template for going down that path.

0

I found the easiest way without playing with anything else was the following.

I ran the application first time with DropAndRecreatedatabase always in the Initilizer.

This created my database for the first time.

Following this I changed this to DropCreateDatabaseIfModelChanges.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.