Friday, August 27, 2010

POCOS IN VS.NET 2010


The Entity Framework enables you to use custom data classes together with your data model without making any modifications to the data classes themselves. This means that you can use "plain-old" CLR objects (POCO), such as existing domain objects, with your data model. These POCO data classes (also known as persistence-ignorant objects), which are mapped to entities that are defined in a data model, support most of the same query, insert, update, and delete behaviors as entity types that are generated by the Entity Data Model tools.


The Entity Framework supports POCO classes ("plain-old" CLR objects). If you want to enable lazy loading for POCO entities and to have the Entity Framework track changes in your classes as the changes occur, your POCO classes must meet the requirements described in this topic so that the Entity Framework can create proxies for your POCO entities during run time. The proxy classes derive from your POCO types.
The Entity Framework creates proxies for POCO entities if the classes meet the requirements described below. POCO entities can have proxy objects that support change tracking or lazy loading. You can have lazy loading proxies without meeting the requirements for change tracking proxies, but if you meet the change tracking proxy requirements, then the lazy loading proxy will be created as well. You can disable lazy loading by setting the LazyLoadingEnabled option to false.

For either of these proxies to be created:

A custom data class must be declared with public access.


A custom data class must not be sealed (NotInheritable in Visual Basic)


A custom data class must not be abstract (MustInherit in Visual Basic).


A custom data class must have a public or protected constructor that does not have parameters. Use a protected constructor without parameters if you want the CreateObject method to be used to create a proxy for the POCO entity. Calling the CreateObject method does not guarantee the creation of the proxy: the POCO class must follow the other requirements that are described in this topic.


The class cannot implement the IEntityWithChangeTracker or IEntityWithRelationships interfaces because the proxy classes implement these interfaces.


The ProxyCreationEnabled option must be set to true.


For lazy loading proxies:

Each navigation property must be declared as public, virtual (Overridable in Visual Basic), and not sealed (NotOverridable in Visual Basic) get accessor. The navigation property defined in the custom data class must have a corresponding navigation property in the conceptual model. For more information, see Loading Related POCO Entities.


For change tracking proxies:

Each property that is mapped to a property of an entity type in the data model must have non-sealed (NotOverridable in Visual Basic), public, and virtual (Overridable in Visual Basic) get and set accessors.


A navigation property that represents the "many" end of a relationship must return a type that implements ICollection, where T is the type of the object at the other end of the relationship.


If you want the proxy type to be created along with your object, use the CreateObject method on the ObjectContext when creating a new object, instead of the new operator.

The ADO.NET Entity Framework enables developers to create data access applications by programming against a conceptual application model instead of programming directly against a relational storage schema. The goal is to decrease the amount of code and maintenance required for data-oriented applications. Entity Framework applications provide the following benefits:
Applications can work in terms of a more application-centric conceptual model, including types with inheritance, complex members, and relationships.
Applications are freed from hard-coded dependencies on a particular data engine or storage schema.
Mappings between the conceptual model and the storage-specific schema can change without changing the application code.
Developers can work with a consistent application object model that can be mapped to various storage schemas, possibly implemented in different database management systems.

To add the ADO.NET Entity Data Model item template
On the Project menu, click Add new item.

In the Templates pane, select ADO.NET Entity Data Model.

Type AdventureWorks.edmx for the model name and then click Add.

The first page of the Entity Data Model Wizard is displayed.

To generate the .edmx file
In the Choose Model Contents dialog box, select Generate from database. Then click Next.

Click the New Connection button.

In the Connection Properties dialog box, type your server name, select the authentication method, type AdventureWorks for the database name, and then click OK.

The Choose Your Data Connections dialog box is updated with your database connection settings.

Ensure that the Save entity connection settings in App.Config as: checkbox is checked and the value is set to AdventureWorksEntities. Then click Next.

In the Choose Your Database Objects dialog box, clear all objects, expand Tables, and select the following table objects:

Address
Contact
Product
SalesOrderHeader
SalesOrderDetail
Click Finish to complete the wizard.

The wizard does the following:

Adds references to the System.Data.Entity, System.Runtime.Serialization, and System.Security namespaces.


Generates the AdventureWorks.edmx file that defines the models and mapping.


Creates a source code file that contains the classes that were generated based on the conceptual model content of the .edmx file. You can view the source code file by expanding the .edmx file in Solution Explorer.

Multiple conceptual models can be mapped to a single storage schema.
Language-integrated query (LINQ) support provides compile-time syntax validation for queries against a conceptual model.
To configure a Visual Studio project to use the AdventureWorks Sales Model
In Solution Explorer, add assembly references to System.Data.Entity.dll and System.Runtime.Serialization.dll.
Add the following model and mapping files to the project:
AdventureWorks.csdl
AdventureWorks.msl
AdventureWorks.ssdl

For information about creating these files, see How to: Manually Define the Model and Mapping Files.
Select the three files you just added to the project directory. On the Project menu, click Include In Project.
Select the three files you added to the project directory. On the Project menu, click Properties.
In the Properties pane, set Copy to Output Directory to Copy if newer.
Open the project's application configuration file (App.config) and add the following connection string:


connectionString="metadata=.\AdventureWorks.csdl|.\AdventureWorks.ssdl|.\AdventureWorks.msl;
provider=System.Data.SqlClient;provider connection string='Data Source=localhost;
Initial Catalog=AdventureWorks;Integrated Security=True;Connection Timeout=60;
multipleactiveresultsets=true'" providerName="System.Data.EntityClient" />

If your project does not have an application configuration file, you can add one by selecting Add New Item from the Project menu, selecting the General category, selecting Application Configuration File, and then clicking Add.

At the command prompt in your project directory, run the appropriate command for your project (with line breaks removed):

"%windir%\Microsoft.NET\Framework\v4.0\edmgen.exe" /mode:EntityClassGeneration
/incsdl:.\AdventureWorks.csdl /outobjectlayer:.\AdventureWorks.Objects.vb /language:VB
This generates an object layer file in either C# or Visual Basic that is based on the conceptual model.

Add the object-layer file generated in the previous step to your project.

In the code page for your application, add the following using statements (Imports in Visual Basic):

Imports System
Imports System.Linq
Imports System.Collections.Generic
Imports System.Text
Imports System.Data
Imports System.Data.Common
Imports System.Data.Objects
Imports System.Data.Objects.DataClasses

If you use the Entity Data Model Wizard in a Visual Studio project, the wizard automatically generates an .edmx file and configures the project to use the Entity Framework . For more information, see How to: Use the Entity Data Model Wizard. You can also manually configure a Visual Studio project to use the Entity Framework . Do this if you have manually defined the model and mapping files or defined them by using the EDM Generator (EdmGen.exe) utility.

The examples in this topic use the model and mapping files for the AdventureWorks Sales model. The AdventureWorks Sales Model is used throughout the task-related topics in the Entity Framework documentation.

No comments:

Post a Comment