Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5931451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:41:48+00:00 2026-05-22T14:41:48+00:00

I have a entity model with the following objects: House Task TaskType I have

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:41:49+00:00Added an answer on May 22, 2026 at 2:41 pm

    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)
     {
        base.OnModelCreating(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 =>
                    {
                        m.ToTable("HouseTaskTypes");
                        m.MapLeftKey("HouseId");
                        m.MapRightKey("TaskTypeId");
                    });
    
        }
    }
    

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say you have a one to one relationship in your entity model. The code
I have a one to many relationship in my core data model. I need
We have an entity framework model with has some inheritance in it. The following
I have the following classes in my Model: public abstract class Entity : IEntity
I have an entity called Task in my Core Data model. The Task entity
In my domain model I have following Classes.A 'UserProfile' has one 'SecurityPrincipal' class SecurityPrincipal{
I have following code to add or update the Entity object. finding the object
I have the following entity model: public class Project { [Key] public int ProjectID
When I have a CoreData entity named, say, 'Book', which has a one-to-one relationship
I have entity model like this (using EclipseLink and JPA 2.0): @Entity class A

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.