What's new in Entity Framework 4? API changes (1 ))

Source: Internet
Author: User
Tags visual studio 2010

If you have been working with the ADO. NET Entity Framework, you have probably been extremely eager to get your hands on the next version that is now part of Visual Studio 2010 and. net 4.0. long referred to as "EF version 2," this version is now called Entity Framework 4 or EF4, to align with. net Framework 4.0 version.

Microsoft released Visual Studio 2010 Beta 1 in mid-May along. net Framework 4.0 Beta 1 and developers have been digging deeply. vs 2010 offers release new features, but this article will focus on new features and improvements in the ADO. NET Entity Framework (EF ). although this is only the second version of EF, it's version number is 4, to align with. net version, therefore recommend people call it EF4.

The changes in EF4 include numerous major improvements to the designer, the addition of model-first design, a new way to customize classes that are generated from the model, the ability to createPocoClasses (plain old CLR objects) that can be used in EF without having to be strongly bound to the EF APIs, and additions that will make using entities into SS processes (e.g. web Services) much easier for developers. I will highlight some of the bigger and more interesting features in the APIs. A future article will highlight designer changes and the tools released in the out of band Feature Pack.

Note that this article is not meant to be a primer on Entity Framework and therefore assumes that you have some experience working with the current version. yet, rather than give only a high level overview of the new features, this article will give you insights into using a number of the new features.

Generating M classes from a model

Once you have designed an Entity Data Model, the designer triggers a code generator, which builds classes from the entities defined in the model. in the first version of Entity Framework, this code generation was handled by code in the system. data. entity. design API. if you wanted to customize the generated code, it meant learning that API and writing your own code generation methods-a somewhat cumbersome task. for EF4, the Team has leveraged a code generator that was introduced in Visual Studio 2008 called T4 (Text Template transformation Toolkit ). seeResourcesSidebar for more information on T4 in general. using T4 templates makes customizing the generated entity classes enormously simpler.

To demonstrate this, I'll begin with a simple model to track my bike rides as shown inFigure 1.

 

 

 Figure 1:Starting with a simple model.

 

There are two steps to this customization. first you need to get access to the default template file that is used to generate the classes. if you look at the msdn documentation on T4, the topic title is "generating artifacts by using text templates. "this knowledge helps make context menu option,Add new artifact generation item...More obvious as the item to choose. Microsoft will change this post-Beta to a more clear description.

When you select this option, the wizard will open the Add Item dialog with the ADO. net entityobject generator selected giving it a default name with the "TT" extension. currently there is only one Template available, but look for more templates to show up in the Future iterations of Visual Studio 2010.

You'll see the new template file in the Solution Explorer as shown inFigure2. Additionally, notice that there is no longer a code file attached to the edmx file. Since the template is now responsible for generating the classes, the class file is attached to the template file.

 

Figure 2:This highlighted T4 template controls the code generation for the bikerides. edmx model.

 

If you select the model in Solution Explorer, You'll see in its property window that the custom tool property is now empty. the default generator, entitymodelcodegenerator, will no longer be used. if you want to revert back to the default, you can just re-enter the default value and EF will take care of the rest.

Now that you have the template file accessible you can begin modifying it. Since my project is C #, the C # template was pulled in but there is also a template written in Visual Basic.

Vs 2010 doesn't have a built-in T4 editor, but you'll find a number of third-party tools that make editing the template file easier. the t4 code listings that follow are formatted using tangible's T4 editor, which I have installed. seeResourcesSidebar for info on T4 editors.

A T4 template has two types of content. the first is straight text, which is written directly to the target file. the second is executable code, called a directive, which is embedded in a pair of angle brackets with hash signs.

The directive will process some logic and write out the text that is the result of that logic. If you are used to working in HTML markup, this may look familiar.

This code snippet shows a directive that CILS a method defined later in the T4 template.

> # Beginregion ("entities"); #>

This directive outputs a starting region tag in the generated code.

# Region entities

It might be helpful to compare the generated code file with the template to get an idea of how the template is built.

Let's focus on the template section that generates a bikeroute class and one of its properties, routename, shown inListing 1.

The snippet below shows the template code that iterates through each entity in the model and writes out the class declaration.

First, you'll see a directive with a comment in it that begins to look at the entities. note that the color-coding for the T4 code is provided a Visual Studio extension by tangible engineering called tangible T4 editor. see the sidebar,Resources-T4, For more info.

<#
////////
//// // Write entitytype classes
////////
Beginregion ("entities ");
Foreach (entitytypewrapper entity
In EDM. sourceentities
. Orderby (E => E. modelname ))
{
#>

After the end of the first directive, the template writes the beginning summary tag to the class file and then uses another directive to determine what goes into the summary comments.

/// <Summary>
/// <# = Entity. summarycomment #>
/// </Summary>

Next, another directive looks to see if there is a longdescription for the entity and if so, writes that out.

<# = Entity. getlongdescriptioncomment

(_ Regionindentlevel) #>

 


Now some more text is written followed by the model namespace, which is determined by another directive.

"
EF4 allows developers to create classes that can be simple classes yet still take part in the Entity Framework.
"

 

[Edmentitytypeattribute (
Namespacename =
"<# = Entity. modelnamespace #> ",
Name = "<# = entity. modelname #>")]
[Serializable ()]
[Datacontractattribute (isreference = true)]

The template continues with more code that reads the XML in the edmx file and generates all of the Code necessary for the entity classes.

Customizing the classes is a matter of modifying the template. For example, by deleting the line of template code that writes out the datacontractattribute you can eliminate the datacontractattribute from every entity class that is created.

With this syntax, You can have enormous control over the outcome of the entity classes that you will use in your application.

&

By: Julia Lerman

Julie Lerman is a Microsoft MVP ,. net mentor and consultant who lives in the hills of Vermont. you can find Julie presenting on data access and other topics at user groups and conferences around the world. julie blogs at thedatafarm.com/blog and is the author of the highly acclaimed programming Entity Framework (O 'Reilly media ). follow Julie on Twitter at julielermanvt.

 

 

 

 

Jlerman@thedatafarm.com

Listing 1: Part of the bikeroute class generated by the default Template
[Edmentitytypeattribute (namespacename = "bikerides ",
Name = "bikeroute")]
[Serializable ()]
[Datacontractattribute (isreference = true)]
[Knowntypeattribute (typeof (mountainbikeroutes)]
Public partial class bikeroute: entityobject
{
[Edmscalarpropertyattribute (entitykeyproperty = false,
Isnullable = false)]
[Datamemberattribute ()]
Public Global: system. String routename
{
Get
{
Return _ routename;
}
Set
{
Onroutenamechanging (value );
Reportpropertychanging ("routename ");
_ Routename = structuralobject. setvalidvalue (
Value, false );
Reportpropertychanged ("routename ");
Onroutenamechanged ();
}

}
Private Global: system. String _ routename;
Partial void onroutenamechanging
(Global: system. string value );
Partial void onroutenamechanged ();

 

Now some more text is written followed by the model namespace, which is determined by another directive.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.