Danh mục

Pro Entity Framework 4 0 Depositfiles_5

Số trang: 26      Loại file: pdf      Dung lượng: 1.10 MB      Lượt xem: 8      Lượt tải: 0    
Jamona

Phí tải xuống: 6,000 VND Tải xuống file đầy đủ (26 trang) 0
Xem trước 3 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Tham khảo tài liệu pro entity framework 4 0 depositfiles_5, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Pro Entity Framework 4 0 Depositfiles_5 CHAPTER 8 ■ T4 CODE GENERATIONFigure 8-14. Custom Tool removed The good thing is that the EF keeps everything intact. You may at first think that the text templatethe EF creates is empty. No way. The EF takes everything it uses for code generation and places it in yournew text template. Double-click the .tt file to open it in the Visual Studio IDE. If you have line numbers enabled, you see that the file is more than 1,250 lines long. At the top of thefile, as shown here, are some nice instructions for modifying the template along with some URLs toprovide further information. Not too shabby:CHAPTER 8 ■ T4 CODE GENERATION information, // see .edmx File Properties (http://go.microsoft.com/fwlink/?LinkId=139299). // *The SourceCsdlPath initialization below must be set to one of the following: // 1) the path of the targeted .edmx or .csdl file // 2) the path of the targeted .edmx or .csdl file relative to the template path // // For more detailed information about using this template, see // How to: Customize Object Layer Code Generation (http://go.microsoft.com/fwlink/?LinkId=139297). // For general information about text templates, see // Generating Artifacts by Using Text Templates (http://go.microsoft.com/fwlink/?LinkId=139298) #> CHAPTER 8 ■ T4 CODE GENERATION }} Next, open the text template EF40Template.tt. Scroll down to line 316, and add the bold code to theend of that line. The bold code includes the comma before IValidator: partial class : , IValidator Next, just below the opening bracket on line 317, add the following code:void IValidator.Validate(){ OnValidate();}partial void OnValidate(); Save the text template. Open the associated class, and scroll down to the entities section. Noticenow that every entity inherits from the IValidator class:#region Entities/// /// No Metadata Documentation available./// [EdmEntityTypeAttribute(NamespaceName=EF40Model, Name=Contact)] [Serializable()] [DataContractAttribute(IsReference=true)]public partial class Contact : EntityObject, IValidator{ void IValidator.Validate() { OnValidate(); } partial void OnValidate()As you can see, T4 templates provide a nice way to customize your entity classes. The reason forimplementing and using T4 for code generation is simply to make it easy to customize the way yourentities are generated. 143CHAPTER 8 ■ T4 CODE GENERATION144CHAPTER 9■■■Model-First DevelopmentIn the last chapter we focused on how to use text templates to customize the generation of the EDM. T4has been incorporated in many facets in EF 4.0, and this chapter will build on that. One of the thingsrequested by EF developers was the ability to generate a database based on the EDM. In the previousversion of EF you could build an EDM starting with an empty model, but you couldn’t do anything withit after that. More specifically, you could not build or create your database based on your EDM. EF 4.0 fixes that problem, and not only lets you build your database based on your EDM, but alsolets you customize the DDL that is generated. This chapter will focus on two aspects of model-firstdesign, the first being the ability to build an EDM and to then create the database based on your EDM.The second part of the chapter will utilize the information you gained in the previous chapter by usingT4 templates and Windows Workflow to customize the output of the DDL.Model-First DesignOne of the most glaring and almost agonizing exclusions from the first release of the Entity Frameworkwas a complete model-first solution. With EF V1, you could create a model from scratch, but you couldnot really do much with mapping and database creation. Anyone who spent any time on the MSDNEntity Framework forums knows that creating the model first was one of the most requested pieces offunctionality. Microsoft listened, and, with Version 4.0 of the Entity Framework, they delivered. With version 4.0 ofthe Entity Framework, you now have a true “model-first” solution. Once you have your conceptualmodel created, you can now derive the storage model, mappings, and database from your conceptualmodel, all from a single menu item on the Designer context menu. From this menu you can generate adatabase schema directly from your model as well as the appropriate mappings. Microsoft also provides the ability to customize the database creation process through T4 templates,giving develo ...

Tài liệu được xem nhiều: