Tag: C#
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
Iterating through an embedded resources file.
by Alec Horn on Dec.16, 2008, under .NET/C#, Coding
So, I decided to try a new format for handling picklists (database mappings) and decided to leave a full code based Enum model with resources files to map to proper names. From there the SQL can be generated during our build process to populate the database. Now, the issue came in when you need to go from a sane (display) value back to the ID or Enum value. In order to get a ResourceReader on an embedded resource, the following works…
ResourceReader v_oReader = new ResourceReader(typeof(T).Assembly.GetManifestResourceStream(typeof(T).FullName + "DataSource.resources")); try { foreach (DictionaryEntry entry in v_oReader) { if (entry.Value.ToString().Equals(p_szDisplayValue)) { return (T)(Enum.Parse(typeof(T), entry.Key.ToString())); } } throw new ApplicationException(String.Format("Unable to match value for '{0}'.", p_szDisplayValue )); } finally { v_oReader.Close(); v_oReader = null; }
Where T is the Enum, and this code assumes that the embedded resx file is stored at Namespace.EnumDataSource.resources