日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

What’s New in Entity Framework 4? API Changes(1)(转)

發布時間:2024/10/12 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 What’s New in Entity Framework 4? API Changes(1)(转) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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 the .NET Framework 4.0 version.

Microsoft released Visual Studio 2010 Beta 1 in mid-May along with .NET Framework 4.0 Beta 1 and many developers have been digging deeply. VS 2010 offers many 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 the .NET version, therefore many 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 create?POCOclasses (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 across 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 Custom 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). See the?Resources?sidebar 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 in?Figure 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 inFigure?2. 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. See the?Resources?sidebar 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 calls 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 in?Listing 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.?

?

轉載于:https://www.cnblogs.com/zjz008/archive/2010/06/01/1748749.html

總結

以上是生活随笔為你收集整理的What’s New in Entity Framework 4? API Changes(1)(转)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 久久久久亚洲av无码专区体验 | 一级爱爱片 | 337p日本欧洲亚洲大胆张筱雨 | 成人性生交大片免费卡看 | 色天天天 | 轻轻色在线观看 | 亚洲视屏一区 | 国产男女无套免费网站 | 天堂亚洲 | 精品国产91久久久久久久妲己 | 青青草国产| 九月婷婷丁香 | 国模吧一区二区三区 | 都市激情校园春色亚洲 | 曰本不卡视频 | 人人草人人干 | 亚洲一区 在线播放 | 桃色一区| 一级片黄色的 | 久久视频免费在线观看 | 91福利视频免费观看 | 日本一区二区不卡在线观看 | 山村大伦淫第1部分阅读小说 | 国产精品男同 | 婷婷av一区二区三区 | 三上悠亚在线观看一区二区 | 成人精品国产免费网站 | 中文字幕一区二区视频 | 美腿丝袜av | 1024手机在线观看 | 色爽爽一区二区三区 | 国产精品区一 | 免费一级特黄 | 亚洲午夜久久久久久久久久久 | 爱爱视频网址 | 日韩av电影网址 | 黄色的网站免费看 | 美女交配 | 久久亚洲中文字幕无码 | 未满十八岁禁止进入 | 娇妻第一次尝试交换的后果 | 18禁裸乳无遮挡啪啪无码免费 | 91精品国产一区二区 | 中文字幕在线视频一区二区三区 | 97人妻一区二区精品免费视频 | 有码一区二区三区 | 国产在线一区二 | 日本日韩欧美 | 久久99国产精品久久99果冻传媒 | www国产 | 穿扒开跪着折磨屁股视频 | 韩国一区二区三区在线观看 | 免费人妻一区二区三区 | 国产高清欧美 | 国产精品免费视频一区二区 | 色中色综合网 | 日韩激情| 亚洲欧美网站 | 台湾150部性三级 | 欧美亚韩一区二区三区 | 一区二区国产在线 | 中文婷婷 | 调教91 | 青青草综合在线 | 亚洲精品乱码久久久久 | 国产激情视频在线观看 | 波多野结衣影片 | 色爽视频| 成人污在线| 天天黄色片 | 波多野一区二区三区 | 夜夜草导航| 青青草福利 | 亚洲精品免费看 | 日韩午夜在线 | 日韩欧美在线视频观看 | 日本三级片在线观看 | 亚洲视频精品一区 | 欧美日韩精品久久 | 国产aⅴ一区二区三区 | 国产精品麻豆果冻传媒在线播放 | 女人被男人躁得好爽免费视频 | 久久人成 | 天天上天天干 | 一级黄网 | 激情视频区 | 国产精品一区二区黑人巨大 | 欧美乱论 | 就是喜欢被他干 | 日韩 欧美 中文 | 日韩特黄毛片 | 打屁股视频网站 | 国产视频亚洲 | 无码人妻久久一区二区三区 | 国产精品日韩精品欧美精品 | 龚玥菲三级露全乳视频 | 亚洲区小说 | 小罗莉极品一线天在线 | 激情久久久 |