O'Reilly logo

Programming Entity Framework, 2nd Edition by Julia Lerman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required