This tutorial will explain how many-to-many relationships appear in the Entity Data Model.
What is a Many-to-Many relationship?
A many-to-many relationship is one where two data tables or entities have multiple rows that are connected to one or more rows in the other table.
A typical example is the EmployeeAddress join table in the AdventureWorks sample database. One employee could have multiple addresses (home, vacation, etc.) and it's possible for one address to apply to multiple employees. In databases, a join table is generally used to create the relationships.
Note that there are additional properties in the join table to track the address type (a customization of the original table), when the joins were created/modified and a unique identifier for each row.
Another example of this is Students and Classes in a database for managing a school. One student can be in many classes and one class can have many students.
Many to Many Relationships in the Entity Data Model
The Entity Data Model Wizard handles these two sets of tables very differently when they are imported into an EDM.
The Employee/Addresses entities mirror the tables -- two entities with a join entity in between. There are navigation properties to traverse between the three entities, Employee.EmployeeAddresses gets you from the Employee to the join entity and from there you can use EmployeesAddresses.Addresses to get the addresses for the Employee you began with.
Using this data in code requires building join queries or queries that drill through multiple navigation properties (eg Employee.EmployeeAddress.Addresses), for example:
PrivateSub employeeaddress() Dim aw AsNew AdventureWorksTModel.AdventureWorksTEntities Dim empaddQuery = From e In aw.Employees, ea In e.EmployeeAddresses _ Select e.EmployeeID, e.HireDate, ea.AddressType, ea.Addresses.City ForEach emp In empaddQuery.OrderBy(Function(e) e.EmployeeID) Console.WriteLine(String.Format("ID {0}:: Hire Date: {1:d}, Address Type: {2}, City {3}", _ emp.EmployeeID, emp.HireDate, emp.AddressType, emp.City)) NextEndSub
Which results with:
ID 1:: Hire Date: 7/31/1996, Address Type: Home , City Monroe ID 1:: Hire Date: 7/31/1996, Address Type: Vacation , City Seattle ID 2:: Hire Date: 2/26/1997, Address Type: Home , City Bellevue ID 2:: Hire Date: 2/26/1997, Address Type: Weekend , City Everett etc...
The student/class entities however, have no join table. Instead they have navigation properties to get to their related data. You can query class.students and you can query for student.classes.
This is a special benefit that the Entity Framework provides when join tables contain only the keys and no additional columns.
Because of this it is possible to traverse directly into the related data without the use of a join entity. Here's an example that uses a nested from clause to create an anoymous type based on the related data.
PrivateSub studentclasses() Dim school AsNew SchoolSampleModel.SchoolSampleEntities Dim studclassQuery = From student In school.students From c In student.classes _ Select student.firstname, student.lastname, c.classname ForEach student In studclassQuery.OrderBy(Function(s) s.lastname & s.firstname) Console.WriteLine(String.Format("Student {0},{1}: {2}", _ student.lastname.TrimEnd, student.firstname.Trim, student.classname)) NextEndSub
Which results with:
Student Doe,John: European History Student Doe,John: Art History Student Robin,Stacey: American History Student Smith,Jane: American History Student Smith,Jane: French Student Williams,Bill: European History Student Williams,Bill: French Student Williams,Bill: Computers 101
Microsoft® SilverlightTM is a cross-browser, cross-platform plug-in for delivering the next generation of .NET based media experiences and rich interactive applications for the Web. Silverlight offers a flexible programming model that supports AJAX, VB, C#, Python, and Ruby, and integrates with existing Web applications. Silverlight supports fast, cost-effective delivery of high-quality video to all major browsers running on the Mac OS or Windows.
In most cases, hosters do not need to perform particular deployments to support Silverlight. However, check for the following basic items that could prevent Silverlight from functioning correctly.
MIME Types
In Windows Server 2008 IIS 7.0
All MIME types needed to support Silverlight are implemented by default in Windows Server 2008 IIS 7.0 and Windows Vista SP1. Windows Vista RTM customers can add mime types by running "IIS Manager", clicking on "Mime Types", then clicking "add" and adding the following mime types:
.xap application/x-silverlight-app
.xaml application/xaml+xml
.xbap application/x-ms-xbap
Alternatively, you can add the following mime types to your %windir%\system32\inetsrv\config\applicationHost.config file in the section.
In Windows Server 2003 IIS 6.0
To enable IIS 6.0 in Windows Server 2003 or IIS7 in Windows Vista RTM with the appropriate MIME Types, add:
.xap application/x-silverlight-app
.xaml application/xaml+xml
.xbap application/x-ms-xbap
Here is a VBS script you could run to enable each of these types:
Const ADS_PROPERTY_UPDATE = 2 ' if WScript.Arguments.Count < 2 then WScript.Echo "Usage: " + WScript.ScriptName + " extension mimetype" WScript.Quit end if ' 'Get the mimemap object. Set MimeMapObj = GetObject("IIS://LocalHost/MimeMap") ' 'Get the mappings from the MimeMap property. aMimeMap = MimeMapObj.GetEx("MimeMap") ' ' Add a new mapping. i = UBound(aMimeMap) + 1 Redim Preserve aMimeMap(i) Set aMimeMap(i) = CreateObject("MimeMap") aMimeMap(i).Extension = WScript.Arguments(0) aMimeMap(i).MimeType = WScript.Arguments(1) MimeMapObj.PutEx ADS_PROPERTY_UPDATE, "MimeMap", aMimeMap MimeMapObj.SetInfo ' WScript.Echo "MimeMap successfully added: " WScript.Echo " Extension: " + WScript.Arguments(0) WScript.Echo " Type: " + WScript.Arguments(1)
If you copy and paste the code above into a VBS file and save it as ADDMIMETYPE.VBS the syntax to add each type would be: