Share via


A first step in a simple WCF Web service using Entity Framework

Hello and Happy New Year 2009 !

Here is a sample code I wrote to show you how to create a WCF Web service which is using Entity Framework to access a SQL 2008 database.

Here are the main steps:

1 - create your SQL database (Products) with the 3 tables : Product, ProductInventory and Location. See the tables here.

2 - create a WCF Web service (ProductsService) with one method: List<Products> GetProducts()

3 - create a class library (DataLibrary) with the ProductsModel (the Entity Model for the 3 tables) and one class : ProductManager (which has the context and the business logic)

4 - in the Web service method call the ProductManager method to obtain the list of products

5 - copy the connexion string from the DataLibrary to the Web service's config file

6 - create a WPF client, add a reference to the Web service and bind the list of products to a listbox

And this is it. See the running program here. The source code is in the attached zip file (Visual Studio 2008).

ProductsService1.zip

Comments

  • Anonymous
    January 05, 2009
    PingBack from http://www.codedstyle.com/a-first-simple-wcf-web-service-using-entity-framework/

  • Anonymous
    January 05, 2009
    Out of curiosity are your Products domain objects (DataContracts) or are they Entiies with DataContract attributes attached to them? Are you using a mult layered design for your services?  

  • Anonymous
    January 06, 2009
    By default, any CLR classes that are generated from an EDM in Visual Studio or using edmgen.exe are XML serializable, binary serializable, and are Data Contracts with the Navigation Properties attributed as DataMembers by default, making it possible to use Entity instances in WCF services. This is a very simple example. In a real world you should have the data layer, the business layer and the presentation/web service layer. Here the business layer is only one class (ProductManager) and it is in the same dll. If you are looking for the best practices see the Web Service Software Factory (the layers it generates).

  • Anonymous
    January 08, 2009
    Couple days ago I started a simple application (WCF Web service using Entity Framework to access a SQL

  • Anonymous
    January 18, 2009
    Does your approach (EF + Web service) make P&P's Web Service Software Factory obsolate?

  • Anonymous
    January 19, 2009
    It's the same thing as the samples when you see DataSets directly binded to the UI. For some small applications this is the good approach, for other applications this is not the recommended one.Nothing tells you not to create your own DataContracts (wrapping/using the EF classes inside), so finally to arrive to the same pattern as in the Web Service Software Factory.Here is only a sample and I choose the fastest way to do it.

  • Anonymous
    February 11, 2009
    Hi Alina, I have read your series of posts on 'A first step in a simple WCF Web service using Entity Framework' and really liked the simple design. In particular the reuse of Entity objects as DataContracts. I am building a WCF webservice for an existing database and I want to generate my entity objects from an existing DB schema. This is fine using your design pattern but if I attempt to use the Web Service Factory I have to redesign the DataContracts using the modeller. Do you know of a way to incorporate the Entity Framework so I can auto generate the DataContracts form the schema. Thanks for your help, James.

  • Anonymous
    February 11, 2009
    Hi It seems that the WSSF is not 100% integrating yet the Entity Framework. I did not have the time to study all the possibilities, but there is a similar discussion (and answers) on the WSSF site: http://www.codeplex.com/servicefactory/Thread/View.aspx?ThreadId=23156 The a developer from the WSSF team suggested that: "since WSSE:ME is pretty extensible, is does support adding a recipe where you can import code defined classes or whatever you want into a model. You may find how to do that in the Extensibility Hands-on labs that can be downloaded from the home page". For more in depth questions you may try to ask the WSSF team.

  • Anonymous
    November 10, 2009
    The comment has been removed

  • Anonymous
    August 05, 2010
    I followed you're steps but in the client I couldn't able to entity tables, but I can see the entities in WCF. But I don't know the reason from WCF to client something is happing and I couldn't able to see my entity table.  Please reply me.ThanksSudhir

  • Anonymous
    August 05, 2010
    Sorry for the delayed answer. For your problems: you have to modify the connection string from the Web.config and App.config files with your local DB name and parameters.