Tag: sql
Entity SQL, Joins and the EntityDataSource with Child Relations
by Alec Horn on Dec.19, 2008, under .NET/C#, ADO.NET, Coding
So, yesterday I actually started working with the Entity framework with a tad more complex queries than the basic select of a single Entity. The first issue I ran into is the flattening of a single Entity 1* Entity relationship. By default the Entity framework and the EntityDataSource actually keeps something similar to an DataTable->ChildDataTable relationship with a prebuilt relationship. Unfortunately the ability to flatten this data is not inherently built in, and join syntax needs to be used:
SELECT E.ID, E.Field, r.Field2 from EntitySet AS E INNER JOIN Entity2Set AS r ON r.Entity = E
Which creates our flattened list of Entities for us based on the defined relationship.
Secondly: EntityDataSource and 1-1 relationships/child objects/picklists. This is much easier to do via the Entity framework and is simply defined via the “Include” parameter.
Ex:
<asp:EntityDataSource ID="x_EntityDataSource" runat="server" ConnectionString="name=DataEntities" DefaultContainerName="DataEntities" Where="it.ID = @EntityID" EntitySetName="EntitySet" EnableUpdate="True" Include="SubEntity"> <WhereParameters> <asp:QueryStringParameter Name="EntityId" QueryStringField="ID" Type="Int32" /> </WhereParameters> </asp:EntityDataSource>
Which provides us with the Entity, and a secondary child Entity which is an FK relationship to a single value.
And, lastly some great posts from the Microsoft team:
http://blogs.msdn.com/diego/archive/2008/05/13/entitydatasource-to-wrap-or-not-to-wrap.aspx
http://blogs.msdn.com/diego/archive/2008/05/24/entitydatasource-s-flattening-of-complex-type-properties.aspx
http://blogs.msdn.com/zlatkom/archive/2007/07/10/entity-sql.aspx