I have an ADO .Net Entity Data Model for my database. In this Model I have three related tables: "Users" Table containing UserId and other details. "Run" Table that contains RunId, ApproverId and other details "RunToUser" that contains UserId and RunId columns
The relationships are as follows: Users.UserId is a one to one relationship to Run.ApproverId User.UserId is many to many with RunToUser.UserId Run.RunId is many to many with RunToUser.RunId
In the Entity Model this is expressed as two tables with 2 separate relationships. the one-to-one relationship and the many-to-many relationship between Users and Run.
I have a ASP.net Formview where I want to edit the data in Run as well as the related data in Users.
Databinding is working in most cases. However when I bind to the Users table as an entitydatasource it always is using the one-to-one relationship association.
How can I force the entitydatasource to use a specific relationship association so that I can bind the data from the one-to-one mapping to a label and the many-to-many to a dataview?
I tried the "EXISTS" in the where clause whichs limits the records but it still insists on always using the one-to-one and only displays the single user in all databound controls. Here is the entitydatasource for the users:
<asp:EntityDataSource ID="edsUsers_DET" runat="server" ConnectionString="name=MyEntities" DefaultContainerName="MyEntities" EntitySetName="Users" Where="EXISTS(SELECT VALUE p FROM it.Run AS p WHERE p.RunID = @RunID)" > <WhereParameters> <asp:ControlParameter ControlID="fvRun" Name="RunID" Type="Int32" /> </WhereParameters> </asp:EntityDataSource>
I assume I will need two entity data sources one for the label and a second for the gridview but until I know determine how to specifiy an association I don't know how to proceed further. Thx, -J
Just following up since I found the answer and forgot to close the question.
I discovered that when you have multiple associations with the same foreign table they gain a numeric suffix by default. So, Run table had a Users and a Users1 association. When you use the "Include" Parameter for the EDS you would need to know which association to use which isnt obvious until you look in the EDS model.
I went thought an renamed all my associations that had the numeric suffixes. My Run table now has a Users and an Approver (instead of Users1) association. This lets me easily decipher which relations ship to include for both LINQ and the EDS declarative portions.
If I wanted to include both associations for the EDS I would do something like:
Hope this helps someone else since it wasn't very obvious to me at first.
If you have a one-to-one relationship in your database, you can build your entity model such that these two tables appear as a single table in your entity model.
If you build it like this your binding becomes much simpler.