Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a little known fact... <strong>Entity Framework + Web Services = :'(</strong></p> <p>There are three(3) approaches that can be taken to solve your problem (namely the XML graph serialization problem... or the lack of it).</p> <p>I will list each approach in order of least development time and complexity of implementation required <em>["Bang-For-Buck"]</em> vs. scalability, maintainability and performance <em>["Future Proofing"]</em>.</p> <ol> <li><p>Create POCO classes for each Entity for projection when sending over the wire. This is the easiest (and monotonous) approach but will solve your problem. I have included a sample at the end.</p></li> <li><p>Use WCF to relay your data. Entity Framework and WCF are like 'brothers from a different mother'. They were designed to work together but share their differences. You will notice that all EF generated Entity Objects are inherently [DataConctract] with all fields being [DataMember]. This makes use of the WCF DataContract Serializer with handles graphs very efficiently and maintains object reference even after deserialization. WCF DataContract Serializer is also proven to be 10% quicker than your default XML Serializer.</p></li> <li><p>Use EF 4.0 Self Tracking Entities (STE). This is still very new but it is workable. In Visual Studio 2010 you are given an option to generate Self Tracking Entities which are designed for N-Tier and SOA. The best thing about STE is the usage of T4 Transformed Text templates to generate the code. The classes generated by T4 is clean and very malleable, giving you plenty of room to insert your logic and validation. <a href="http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-self-tracking-entities-for-the-entity-framework.aspx" rel="nofollow noreferrer">Here</a> is a link to STE tutorial to get your started.</p></li> </ol> <p>Best of luck and I hope you find the best approach for you.</p> <p>POCO example.</p> <pre><code>public class CustomerPOCO { public int ID { get; set; } public string Name { get; set; } public List&lt;SubLocationPOCO&gt; SubLocations { get; set; } // ... #region Ctors public CustomerPOCO() { } public CustomerPOCO(Customer customer) { // Inits if (customer.SubLocations != null) SubLocations = customer.SubLocations.Select(sl =&gt; new SubLocationPOCO(sl)).ToList(); } #endregion } public class SubLocationPOCO { public int ID { get; set; } public string Name { get; set; } #region Ctors public SubLocationPOCO() { } public SubLocationPOCO(SubLocation subLocation) { // Inits } #endregion } </code></pre> <p>And your [WebMethod] is something like this.</p> <pre><code>[WebMethod] public CustomerPOCO GetCustomerByID(int customerID) { using (var context = new CustomerContext()) { var customer = (from customer in context.Customers.Include("SubLocations") where customer.ID == customerID select new CustomerPOCO(customer)).FirstOrDefault(); return customer; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload