Search code examples
asp.netentity-frameworklistviewentity-framework-4entitydatasource

Relationship change from one-many into Many to many need to update listview


I have a distribution table with a pk of DistributionID and a recipients table with RecipientID as pk. This table used to be 1 to many but now needs to change to a many to many with an intermedate table.

I have an EntityDataSource that supplies a listview that allows for simple manipulation of the distribution list.

<asp:EntityDataSource ID="edsRecipients" runat="server" ConnectionString="name=DistributionEntities" DefaultContainerName="DistributionEntities" 
                        EnableDelete="True" EnableFlattening="False" EnableInsert="True" EnableUpdate="True" EntitySetName="Recipients"
                        Where="it.[DistributionID]=@DistributionID">
    <WhereParameters>
        <asp:ControlParameter ControlID="ddlSelectDistributionList" ConvertEmptyStringToNull="true" DbType="Int32" Name="DistributionID" />
    </WhereParameters>
</asp:EntityDataSource>  

Is there any way i can change the where clause to work with the new table New table is named DistributionRecipients if that is needed. If not is there some way i can bind the Distribuion.Recipients of the EF Class to the List view in the code behind that will work with the automatic edit and delete functionality or will i need to add code to handle them?


Solution

  • You can modify the where attribute of your EntityDataSource to include a sub-query on the Distributions navigation property of the Recipients entity:

    EXISTS(SELECT Distributions.DistributionID FROM it.Distributions WHERE Distributions.DistributionID=@DistributionID)