Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF 4.1 Code First and Existing Database and .NET Membership

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?

like image 251
Mike Avatar asked Mar 28 '11 15:03

Mike


People also ask

How do you use code first when an existing database schema?

To use code-first for an existing database, right click on your project in Visual Studio -> Add -> New Item.. Select ADO.NET Entity Data Model in the Add New Item dialog box and specify the model name (this will be a context class name) and click on Add. This will open the Entity Data Model wizard as shown below.

How do I code my first migration to an existing database?

Run the Add-Migration InitialCreate command in Package Manager Console. This creates a migration to create the existing schema. Comment out all code in the Up method of the newly created migration. This will allow us to 'apply' the migration to the local database without trying to recreate all the tables etc.

What is the difference between code first and database first?

In the code first approach, the programmer has to write the classes with the required properties first, while in the database first approach, the programmer has to create first the database using GUI.


2 Answers

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.

like image 73
Ladislav Mrnka Avatar answered Oct 08 '22 23:10

Ladislav Mrnka


Found a easier workaround here. I hope this helps.

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

like image 20
cbrcoder Avatar answered Oct 08 '22 22:10

cbrcoder