Chapter 11. Customizing Entities

In previous chapters, we worked with entity classes and the context class that were generated from the model. The methods and events available to you for these classes were limited to the methods and events derived from their base classes: EntityObject and ObjectContext, as well as those inserted by the code generation.

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 use partial classes to add new logic to entities or override their existing logic. You will also learn how to change the rules for code generation and in doing so create classes from the model that are more to your liking.

In Chapter 13, you will learn how you can use your own custom classes in 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. ...

Get Programming Entity Framework, 2nd Edition 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.