Danh mục

Pro Entity Framework 4.0 - Apress_6

Số trang: 26      Loại file: pdf      Dung lượng: 1.08 MB      Lượt xem: 11      Lượt tải: 0    
tailieu_vip

Hỗ trợ phí lưu trữ khi tải xuống: 18,000 VND Tải xuống file đầy đủ (26 trang) 0

Báo xấu

Xem trước 3 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Khi Microsoft thiết lập để làm cho Entity Framework linh hoạt hơn, mục tiêu của họ là cung cấp cho các nhà phát triển với một môi trường mà ở đó họ có thể tạo ra mô hình của họ từ một trong những cách tiếp cận của họ. Các phương pháp tiếp cận đầu tiên, đã tồn tại kể từ khi phát hành ban đầu của EF,
Nội dung trích xuất từ tài liệu:
Pro Entity Framework 4.0 - Apress_6C H A P T E R 10■■■Code-Only DevelopmentWhen Microsoft set about to make the Entity Framework more flexible, their goal was to providedevelopers with an environment in which they could create their model from one of their approaches.The first approach, which has existed since the initial release of EF, is the ability to generate your modelfrom a database. Many of the chapters in this book have covered the new features and concepts that aretied to this approach. Chapter 9 covered the second approach, model-first, which lets developers start with a conceptualmodel and generate their database based on that conceptual model. This approach also lets developerscustomize DDL generation through T4 templates and Windows Workflow, giving them the utmostflexibility and control over the creation and generation of the DDL. This is truly a “model-first” solution.This ability to customize is a giant step over EF 3.5, in which you could define your conceptual model inEF but not do anything beyond that, such as generate your database. This chapter will cover the third approach, the code-only approach, which provides developers theability to use the Entity Framework using POCO entities and without an EDMX file. This approach letsdevelopers view code as their model. There is a lot of discussion around the code-only approach, such aswhether code-only will contain much of the functionality found in a standard EDM model approach.The goal from the outset was to ensure that most, if not all, of the functionality found in the other twoapproaches (model-first, database-first) is found in the code-only approach. This includes topics such asdeferred/lazy loading, change tracking, and complex types. And let’s not forget new foreign keyassociation support. This is included as well. In this chapter we will cover the code-only approach and discuss many aspects of building an EFproject using code-only. We will build a simple example, and we will also discuss some of the otheraspects that you can add to your code-only project, including deferred/lazy loading, complex types, andchange tracking. Let’s get started.Getting Started with Code-OnlyThe first thing you need to do is download a feature preview from the Microsoft website. In your favoritebrowser, go to the following URL:http://www.microsoft.com/downloads/details.aspx?familyid=13FDFCE4-7F92-438F-8058-B5B4041D0F01&displaylang=en The ADO.NET Entity Framework Feature CTP is a set of features that adds additional features andcomponents to EF 4.0. From this website, click the Download button, which downloads a file calledEF4FeatureCTP2.exe. The ADO.NET Entity Framework Feature CTP installs the following components: Templates for self-tracking entities (used for N-Tier support...we’ll use this in • Chapter 11) 167CHAPTER 10 ■ CODE-ONLY DEVELOPMENT Code-only programming model used with the Entity Data Model • Make sure Visual Studio is not open when you run the installation. When the installation is complete, we are ready to begin. Creating the Data Project In Visual Studio 2010, create a new Class Library project. I called mine CodeOnlyData, as you can see in Figure 10-1. The CodeOnlyData project is the data project that will contain the POCO classes that will mimic the EDM. Figure 10-1. Create class library project Once the project has been created, delete the Class1.cs class and add two additional classes: Contact.cs • Employee.cs • We are ready to add code. The Contact and Employee classes are our POCO classes and are essentially called POCO entities. Let’s work with the Contact class first. Open the Contact class and replace everything in that class with the following: using System; using System.Data; using System.Collections.Generic; using System.Linq; using System.Text;168 CHAPTER 10 ■ CODE-ONLY DEVELOPMENTnamespace CodeOnlyData{ public class Contact { public Contact() { } public int ContactID { get; set; } public bool NameStyle { get; set; } public string Title { get; set; } public string FirstName { get; set; } public string MiddleName { get; set; } public string LastName { get; set; } public string Suffix { get; set; } public string EmailAddress { get; set; } public int EmailPromotion { get; set; } public string Phone { get; set; } public string PasswordHash { get; set; } public string Passwor ...

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