DOC

Entity Framework 4.0 and WCF Data Services 4.0 in Visual Studio 2010

By Allen Harrison,2014-04-03 08:50
9 views 0
Elisa Flasko takes you through a sample weblog application to demonstrate how new improvements in Entity Framework and WCF Data Services work together to simplify the way data is modeled, consumed and produced in Visual Studio 2010.

    Entity Framework 4.0 and WCF Data Services 4.0 in Visual Studio 2010

    Elisa Flasko

    Download the Code Sample

    Among its many new improvements, Visual Studio 2010 introduces the much-anticipated Entity Framework 4.0 and WCF Data Services 4.0 (formerly ADO.NET Data Services), which together simplify how you model, consume and produce data.

    Entity Framework 4.0 (EF 4.0) focuses on enabling and simplifying two primary scenarios: domain-centric application development and the traditional data-centric “forms over data.” It introduces features such as model first

    development, which allows you to create a model and have customized T-SQL generated for you; support for persistence ignorance; foreign keys; lazy loading and custom code generation for entities.

    WCF Data Services 4.0 focuses on enabling updates to the Open Data Protocol (odata.org) and on its new features, including two-way data binding for Windows Presentation Foundation (WPF) and Silverlight, row count, server-driven paging, enhanced binary large object support and support for projections.

    Using a sample weblog application (MyBlog), I will explore the new features in both EF and WCF Data Services and explain how the technologies work together to simplify the way data is modeled and consumed. This sample application will have both an ASP.NET Web application that provides a read-only view of blog posts, and a Silverlight blog administrator client that allows the blog owner to edit posts. I will begin the application using model first to create an Entity Data Model (EDM), and then generate a database and the code to interact with that database. This sample will also make use of the ADO.NET Data Services Update for Silverlight 3 CTP 3.

    Getting Started with EF 4.0

    I’ll begin with the ASP.NET Web Application project. (My application is called BlogModel; you can download the accompanying code at

    code.msdn.microsoft.com/mag201004VSData.) To get started with EF, I use the Add New Item wizard to add an ADO.NET EDM, and select an Empty model that I’ll call BlogModel as well. By right-clicking on the empty Designer

    surface and choosing Properties, you can see the default Entity Container name, BlogModelContainer, in this case. First I’ll change the name to BlogContext, and then I’ll create the model.

    MyBlog requires three Entities I’ll call Blog, Post and Tag, as shown in Figure 1. To create them, I drag an Entity from the toolbox to the design surface, then

    right-click and select Properties to edit the entity properties. I’ll also need a few

    scalar properties on each of the entities (right-click on the entity and select Add

    | Scalar Property).

    Figure 1 Blog, Post and Tag Entities and Associated Property Settings Foreign Key Support in EF 4.0

    Next, I’ll add relationships between these entities. Right-click on the design surface and select Add | Association, as shown in Figure 2. EF now supports foreign keys, allowing the inclusion of foreign key properties on an entity.

    Notice that adding the relationship added a BlogBlogID property (the foreign

    key) to the Post entity.

Figure 2 Associations Between Blog, Post and Tag Entities

    The inclusion of foreign key properties on entities simplifies a number of key

    coding patterns, including data binding, dynamic data, concurrency control and

    n-tier development. For example, if I’m databinding a grid that shows products

    and I have the CategoryID (a foreign key value) in a grid but I don’t have the corresponding Category object, the foreign key support in EF means I no

    longer need to take on the cost of a query to separately pull back the Category

    object.

    Model First with EF 4.0

    Now that the model is built (see Figure 3), the application needs a database.

    In this case, MyBlog is a new application and doesn’t yet have a database. I

    don’t want to create the database myself; I’d rather just have it done for

    meand I can. With model first in EF 4.0, Visual Studio can now generate not

    only custom code for the entities, but also a database based on the model just

    created.

    Figure 3 Blog Model First, I need to create the empty database to which I will apply the generated

    schema. To do this, I open Server Explorer, right-click on the Data

    Connections node and select Create New SQL Server Database (see Figure

    4). With the empty database created, I right-click on the model design surface

    and select Generate Database from Model. Walking through the Generate

    Database wizard creates a BlogModel.edmx.sql file. With this new file open,

    it’s simple to right-click on the file and execute the SQL script to create the schema for my database.

Figure 4 Create a New Empty Database and Generate Database Schema

    from EDM Custom Code Generation with EF 4.0

    At this point, there are a number of next steps possible, one of which is to customize the code that EF generates, using T4 Templates. In Visual Studio 2008 SP1, although EF provided some hooks for customizing code generation, it was relatively inflexible and hard to use. EF 4.0 now leverages T4 Templates, providing a much simpler, more flexible and more powerful way to customize generated code.

    To add a new T4 Template to the project, right-click on the Entity Designer surface and select Add Code Generation Item. From here, choose any currently installed template to use as a starting point, or view the templates available in the Online Gallery. To use the default EF template as the starting point for this project, I’ll choose the ADO.NET EntityObject Generator template;

    by default the template is called Model1.tt. By adding a code generation template in this manner, EF automatically disables default code generation for the model. The generated code is removed from BlogModel.Designer.cs and now exists in Model1.cs. At this point, the template can be edited to customize the entities it will generate, and every time the .tt file is saved, the dependent code will be regenerated. For more information on editing and using T4 Templates with EF 4.0, check out the ADO.NET Team blog at

    blogs.msdn.com/adonet.

    Using POCO Entities with EF 4.0

Visual Studio 2008 SP1 imposed a number of restrictions on entity classes that

    made it difficult, to say the least, to build classes that were truly independent of

    persistence concerns. One of the most requested features in EF 4.0 is the

    ability to create Plain Old CLR Object (POCO) types for entities that work with

    EF and don’t impose the type of restrictions in Visual Studio 2008 SP1.

    Let’s go back to the MyBlog sample. I’m going to create POCO objects for the

    three entitiesBlog, Post and Tag. First, code generation needs to be turned

    off and I need to remove the .tt file I added in the last section. To check out the

    properties of the model, right-click on the Entity Designer surface. As shown in

    Figure 5, there is a property named Code Generation Strategy that needs to

    be set to None to turn off code generation.

    Figure 5 Code Generation Strategy Property Note that this property is set to None automatically when you add a Code

    Generation Item (T4 Template) to a project. If the project currently has a .tt file

    included, you will need to remove it before using POCO objects. From here,

    the classes for the POCO objects can be addedBlog.cs, Post.cs and Tag.cs,

    as shown in Figures 6, 7 and 8.

    Figure 6 POCO Object for Blog Entity public class Blog

{

     public intBlogID

     {

     get;

     set;

     }

     public string Name

     {

     get;

     set;

     }

     public string Owner

     {

     get;

     set;

     }

     public List Posts

     {

     get { return _posts; }

     set { _posts = value; }

     }

     List _posts = new List();

}

    Figure 7 POCO Object for Tag Entity

    public class Tag

{

     public int TagID

     {

     get;

     set;

     }

     public string Name

     {

     get;

     set;

     }

     public List Posts

     {

     get { return _posts; }

     set { _posts = value; }

     }

     List _posts = new List();

}

    Figure 8 POCO Object for Post Entity

    public class Post

{

     public int PostID

     {

     get;

     set;

     }

     public DateTime CreatedDate

     {

     get;

     set;

     }

     public DateTime ModifiedDate

     {

     get;

     set;

     }

     public string Title

     {

     get;

     set;

     }

     public string PostContent

     {

     get;

     set;

     }

     public Blog Blog

     {

     get;

     set;

     }

     public int BlogBlogID

     {

     get;

     set;

     }

     public Boolean Public

     {

     get;

     set;

     }

     public List Tags

     {

     get { return _tags; }

Report this document

For any questions or suggestions please email
cust-service@docsford.com