1. Trang chủ
  2. » Công Nghệ Thông Tin

Lập trình .net 4.0 và visual studio 2010 part 28 docx

6 311 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 6
Dung lượng 257,56 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

In EFv1 if you had a table called Film, then your entities would be called Film, and entity sets of Film would also be called Film, which made querying not as readable as it could be.. F

Trang 1

(domain-driven design) I believe this release has resolved a number of the original concerns and introduced some great new features Let’s see what they changed

EDM Designer Changes

VS2010 contains a new Model Browser window that allows you to easily navigate your EDM (Figure 8-10) It

is worth noting that previously in EF if an entity was removed from the model and subsequently brought back by using the wizard's update model functionality, then it would not be recreated This was because

a reference to the original object was still held in the CSDL file, so EF believed it still existed

In VS2010 the Model Browser window now contains a new facility that allows you to remove the CSDL entries as well To do this, simply right-click on the item you want to remove under the Store node and select Delete This will then remove the entity from the storage model

Figure 8-10 New model browser window

Trang 2

Performance

The EF team has fine-tuned the performance of EF One area that was improved is query generation

Previously parameter length was passed into queries This was a bad decision; parameters could vary in length, which would prevent SQL Server from utilizing the query cache In EF4 parameter length is no

longer passed so the query cache can be utilized The team has also tweaked how joins are made and

removed unnecessary IsNull() calls

Pluralization

Pluralization is a new feature in EF4 that generates more readable entity names In EFv1 if you had a

table called Film, then your entities would be called Film, and entity sets of Film would also be called

Film, which made querying not as readable as it could be Thus many developers would manually

rename entity sets of Film to be called Films, which could be pretty boring

In EF4 if you select to use the pluralization option when creating the model, then Film’s entity set

will be automatically named as Films

Note that the new pluralization feature doesn’t just stick an -s on the end of the table name It is

cleverer than that; for example, it knows when to append or remove -s, or when to modify the ending of

a word to -ies (e.g., Category becomes Categories) Pluralization currently only works with U.S English, although there is an option to override this and provide your own pluralization provider

The pluralization service is contained in System.Data.Entity.Design.dll (which is a shame as this would be useful elsewhere) and has a static method called CreateService() For more information

please see

http://www.danrigsby.com/blog/index.php/2009/05/19/entity-framework-40-pluralization/

Deferred/Lazy Loading

Lazy loading is a coding technique that minimizes resource usage and database access by not loading an entity until it is actually accessed In EF4 lazy loading is switched on by default If you want to turn lazy loading off, then set the ContextOptions.LazyLoadingEnabled option to false in the context class:

ctx.ContextOptions.LazyLoadingEnabled = false;

Eager Loading

While we are looking at lazy loading it's worth mentioning its evil twin, eager loading Eager loading is

the exact opposite to lazy loading and means pre-loading an entity before you need it This can increase the responsiveness of your application if the initialization is spread out over your application's lifetime Julia Lerman has a great post that describes a number of ways to implement eager loading and measures the effect it has on performance:

http://thedatafarm.com/blog/data-access/the-cost-of-eager-loading-in-entity-framework/

Complex Type Designer Support

If your entities contain many fields then it can be useful to subdivide them into types For example, a

Patient entity in a medical system may have a huge amount of properties, but using complex types we could divide this information up like so:

Trang 3

Patient.Demographic.FirstName

Patient.Demographic.Age

Patient.Demographic.LastName

Patient.Clinical.BloodType

Patient.Financial.InsurerName

Previously, if you wanted to accomplish this it was necessary to manually edit the CSDL, but as of EF4 you can accomplish this in the designer Let’s see how to work with this feature with our Film entity

1 Select the Film entity

2 Hold down the Ctrl key and select the Description and Length properties (Figure 8-11)

3 Right-click and select the Refactor into New Complex Type option on the context menu

Figure 8-11 Refactoring description and Length into a complex type

4 VS will create a new property called ComplexProperty: rename this property to Detail

5 If you open Program.cs you will now be able to access these properties using code similar to the following:

Film Film = new Film();

Film.Detail.Description = "New film";

Film.Detail.Length = 200;

Trang 4

 TIP To undo this change, remove the Film table from the model designer and then add it in again by

right-clicking and selecting Update Model from Database

Complex Types from Stored Procedures

The function import wizard will now create complex types from stored procedures For example, let's

imagine we wanted to add a method to our Film entity to return information about some of the crew,

which is retrieved using the following stored procedure (mocked up for ease of use):

CREATE PROCEDURE FilmGetCrewInfo

@filmID int

AS

SELECT

'James Cameron' as Director,

'Arnold Schwarzenegger' as LeadActor1,

'Linda Hamilton' as LeadActor2

1 Go to the Model Browser window (tab next to Solution Explorer)

2 Right-click on the Complex Types folder and add a new complex type called FilmCrew

3 Right-click on the newly created complex type and add three new string scalar properties called Director, LeadActor1, and LeadActor2 (Figure 8-12)

Figure 8-12 Creating a new complex type

4 Open Chapter8.Model.edmx and on the designer surface right-click and select the Update Model from Database option

Trang 5

5 Under the Stored Procedures node select the FilmGetCrewInfo stored procedure and click Finish

6 Right-click on the designer surface and select AddFunction Import to bring up the screen shown in Figure 8-13 (I also clicked Get Column Information button when completed the other information to populate the stored procedure column information section)

Figure 8-13 Add function import screen

7 Enter the function import name GetCrewInfo

8 Select the stored procedure name FilmGetCrewInfo

9 Select Complex in the Returns a Collection Of radio button options and then FilmCrew on the dropdown (notice how you have the option to create a complex type from the results of the stored procedure)

10 Click OK The EF designer will now have added this function to the context where it can be accessed

as follows (note you could then move this into your entity using partial classes):

var crew = ctx.GetCrewInfo(1);

Model Defined Functions

Model defined functions allow you to define reusable functions at a model level To create them at present you must modify the edmx file directly, although this will probably change in future versions of

Trang 6

EF In our convoluted example we will create a new property for our Film entity that will return the Film title and description separated by a space

1 Right-click on the Chapter8Model.edmx file and select Open With

2 Select XML Editor

3 Find the following section:

<edmx:ConceptualModels>

<Schema Namespace="BookModel" Alias="Self"

xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation"

xmlns="http://schemas.microsoft.com/ado/2008/09/edm">

4 Add the following inside the previous section:

<Function Name="LongFilmDescription" ReturnType="Edm.String">

<Parameter Name="Film" Type="BookModel.Film">

</Parameter>

<DefiningExpression>

Trim(Film.Title) + " " + Film.Description

</DefiningExpression>

</Function>

5 Open Program.cs and add the following using directive:

using System.Data.Objects.DataClasses;

6 Unfortunately LINQ to Entities doesn’t yet know about the LongFilmDescription function, so

we have to tell it by creating a static class decorated with the [EdmFunction] attribute to allow us

to access it Add the following code in Program.cs

public static class MDF

{

[EdmFunction("BookModel", "LongFilmDescription")]

public static string LongFilmDescription(Film f)

{

throw new NotSupportedException("This function can only be used in a query"); }

}

7 Once this is done we can now utilize our function in L2E queries as follows:

var query = from f in ctx.Films

select new { FullName = MDF.LongFilmDescription(f) };

Model First Generation

EF4 allows you to create your entity model in Visual Studio and use it to generate and update database structure At the time of writing this works only with SQL Server This facility is great for users unfamiliar with SQL or in situations where you do not have access to the database

1 Create a new C# console project called Chapter8.ModelFirst

2 Add a new ADO.NET Entity Data Model called CustomerModel

3 Click Next

Ngày đăng: 01/07/2014, 21:20

TỪ KHÓA LIÊN QUAN