Take the 2-minute tour ×

I have a entity model with the following objects:

  • House
  • Task
  • TaskType

I have the following relationships: House <1>----<> Task (One to many) Task <>----<1> TaskType (Many to one)

Now, I wanted to add a many to many relationship between House and TaskType, to set which TaskTypes are available for a house.

What is the correct way to do this in Visual Studio 2010 without losing data in the database.

If I do this on a brand new model, which doesn't have any database generated yet, it works fine, but if I try to add it after I've generated the database the first time, I will loose all my data since the genereated SQL drops all tables.

If I try to create a table manually in the DB called HouseTaskTypes with two columns (House_Id and TaskType_Id) with foreign keys to House and TaskTypes, it looks weird when I update the model from the database.

I can probably get it to work with some manual adjustments, but I'd like to know what the correct way is of adding a many-to-many association/relationship in an already existing Entity Framework model.

All ideas are appreciated!

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Migrating an existing database schema to a new one is not supported in EF 4.1. From msdn:

Code First does not support migration of an existing database schema. The Entity Framework 4.1 does support dropping and re-creating a database schema when the model changes through using database initializers. The following initializers are supported: CreateDatabaseIfNotExist, DropCreateDatabaseAlways, and DropCreateDatabaseIfModelChanges

So, to solve your problem, I would

  • set the intializer to CreateDatabaseIfNotExist, or turn it off
  • manually add the table to the database
  • manually add the navigation properties to your model
  • manually map the many-to-many relationship (see below)
  • then add any data to the new table either through your app or manually

To manually map the relationship add the following method to your DBContext

 class protected override void OnModelCreating(DbModelBuilder modelBuilder)

    modelBuilder.Configurations.Add(new HouseConfiguration());   `

Then, in the same, or reachable, namespace as your DBContext class, add a configuration class for one side of the many-to-many relationship. The purpose of this class is do the actual mapping. I typically have a separate namespace just for these configuration classes.

class HouseConfiguration : EntityTypeConfiguration<House>
    public HouseConfiguration()
        // many-to-many w/tasktypes
        this.HasMany(h => h.TaskTypes)
            .WithMany(tt => tt.Houses) 
            .Map(m =>


You will have to double check the property names/key names, but this should do it.

share|improve this answer
That's what I've been trying to do. I first added a "in-between" table with two columns, one for the House_id and one for TaskType_id. This was done in the SQL Server (2008 R2 Express) Then I tried adding a many-to-many association between the "House" entity and the "TaskType" entity in the Entity Model designer in Visual Studio. Problem is that I get the error "No mapping specified for the following entityset/associationset". And since the table does not appear in the "Model Browser -> Store -> Tables / Views" section, I cannot map to it. –  Sverker84 May 20 '11 at 8:25
@Sverker84 Edited my answer –  Steve Mallory May 20 '11 at 20:09
add comment

Your Answer


