Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use Database Migrations and EF 4.3 with a code first approach to create relationship tables?
    primarykey
    data
    text
    <p>I have been trying to get a handle on using MVC 4.0 with EF 4.3 and Database Migrations as introduced by Scott Gu at Tech Days and have run into an issue with the database that is created when a many to many relationship exists.</p> <p><a href="http://channel9.msdn.com/Events/TechDays/Techdays-2012-the-Netherlands/2364" rel="nofollow">http://channel9.msdn.com/Events/TechDays/Techdays-2012-the-Netherlands/2364</a> starts at 21:14</p> <p>Included are the snippets of code that were used to generate the database as shown in the image below.</p> <pre><code>public class Trip { public int ID { get; set; } public string Name { get; set; } public DateTime Begin { get; set; } public DateTime End { get; set; } public Location HomeCountry { get; set; } //public IList&lt;Location&gt; Itinerary { get; set; } public ICollection&lt;Location&gt; Itinerary { get; set; } public Trip() { this.Itinerary = new HashSet&lt;Location&gt;(); } } public class Location { public int ID { get; set; } public string Name { get; set; } public ICollection&lt;Trip&gt; Trips { get; set; } public Location() { this.Trips = new HashSet&lt;Trip&gt;(); } } </code></pre> <p>This was the resulting database. (I would have posted an image, but I am unable to post an image, still being a new user)</p> <pre> Tables Locations PK ID (int) Name (varchar) Trip_ID (int) Trips PK ID (int) Name (varchar) Begin (datetime) End (datetime) HomeCountry_ID (int) </pre> <p>I guess I would have expected a third relationship table to be created for the many to many relationship between Locations and Trips (many Locations can apply to many Trips), versus, adding the Trip_Id column to the Locations table. Does anyone know what I did wrong here? Is there a way to get the data base automation to create these tables correctly?</p> <p>Thanks in advance for all of your help!</p> <p><b>Update:</b> I found the following link, but I am still unable to get this to work using EF 4.3. I also edited my code snippets to reflect the following post. </p> <p><a href="http://www.codeproject.com/Articles/234606/Creating-a-Many-To-Many-Mapping-Using-Code-First" rel="nofollow">http://www.codeproject.com/Articles/234606/Creating-a-Many-To-Many-Mapping-Using-Code-First</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload