25 Mar 2013

Entity Framework 5 Many to Many relationship setup

Sample Solution Download Link
How to setup entity framework objects for many to many relationship.
I had a problem trying to get this to work but after I managed to get it working
I decided to post the solution here with a hope that it will help someone someday.

To begin.
1. Download the solution (it is a vs-2012 solution) & unzip accordingly
2. Create a database called (EdkSamples)
3. Create a login (user: developer, password: letmein$123)
4. Create database tables using the scripts
    Edikgale.Samples.Data\Database Setup\DatabaseCreate.sql
5. Initialise the database data with the script in folder
    Edikgale.Samples.Data\Database Setup\DataSetup.sql
6. To see how this works simply run the test in
    EdwardDikgale\Edikgale.Samples.Data.Tests\EdikgaleEfContextTest.cs

Note
1. The test project uses a connection string defined in App.config which
you may wish to adapt accordingly.
2. The linking table is not modelled as a poco object because there is no need to unless
you happen to have some extra properties on it.

if you have any problems feel free to email me: edward.dikgale@gmail.com

I hope this helps you in some way
Ed


posted by: Edward Dikgale

1 comment:

  1. Thanks for this Ed. I haven't looked at your solution yet, but when I've modelled many-to-many in EF it's ended up with two POCO objects referencing each other as collections. How come you're modelling the linking table itself?

    ReplyDelete