WCF Service with SQL Classes vs Entity Framework


I was creating a simple service to return some data from a Pubs (yes, Pubs) instance on my machine.  The service was created to serve data to a SilverLigh client that I am creating.  In my first attempt I used SQL Classes to generate entities against which I wrote LINQ.  I kept receiving an error and with some digging (and some tracking) we discovered that the root of the problem is that the SQL Classes generated a circular reference.  If you are familiar with pubs you can imagine this to exist between author and titleauthor or title and titleauthor.  Out of curiosity, I generated an entity model for the same database.  I wrote a little bit of code to wire it up and ran it with unexpected success.  Why did it work for one and not the other?  The answer was much simpler to find than the original serialization problem.  My first guess was wrong as to the attributes that made a difference.  However, what is represented below is now accurate.


I went through the generated code looking at the entity definitions. In the SQL Classes code I found the following declaration for titleauthor as a property of author:


[Association(Name="author_titleauthor", Storage="_titleauthors", ThisKey="au_id", OtherKey="au_id")]
public EntitySet<titleauthor> titleauthors {...}

If I crack open the .cs generated by the entity framework I see:



[global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName="pubsModel", Name="titleauthor")]
[
global::System.Runtime.Serialization.DataContractAttribute(IsReference=true)]
[
global::System.Serializable()] public partial class titleauthor : global::System.Data.Objects.DataClasses.EntityObject


So, the entity framework went ahead and added the DataContractAttribute(IsReference=true) to the declaration which fixes up the object for serialization by the DataContract serializer. 


As an additional note, I'm told by one of my colleagues that during the creation of the SQL Classes that there is an option to make the classes generated by the wizard serializable and thus they too will work without modification as a DataContract.

Comments (1)

  1. Thank you for submitting this cool story – Trackback from DotNetShoutout

Skip to main content