Chapter 10. Customizing Entities

In previous chapters, we worked with entity classes that the Entity Framework’s code generator created. The methods and events available to you for these classes were limited to the methods and events derived from their base classes: ObjectContext and EntityObject.

Because the purpose of entities is to provide data schema, they contain little in the way of business logic. This is great for getting started, but many applications will need more.

The extensibility of the Entity Framework provides a number of ways to not only add your own logic, but also use your own classes and plug them into an ObjectContext.

In this chapter, you’ll learn how to add new logic to entities or override their existing logic using partial classes, a feature introduced in .NET 2.0.

In Chapter 19, you will learn how you can tie your own custom classes into the Entity Framework.

Partial Classes

All of the classes that are generated from an Entity Data Model (EDM)—the class that inherits from ObjectContext as well as the entities themselves—are partial classes. Partial classes allow you to break a class into multiple code files, and they are especially valuable when you want to make changes to generated code. Without partial classes, modifications to generated code will be lost whenever the generation is performed again. Rather than making your changes directly in the generated code, you can make them in a separate file that will not be touched when the code generator performs its ...

Get Programming Entity Framework now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.