When it comes to brand new data applications, there are two broad ways to approach building the application: design the object model first and the database later, or build the database first and the object model later. The first option is often referred to as top-down design, because you’re building application components first that sit on top of other layers including the database. The second option is bottom-up design because the underlying foundation of the application, the database, comes first. Top-down design places the business rules and architecture of the application as the highest value, and bottom-up design puts the emphasis on strong data organization. Neither methodology is necessarily better than the other: each one fits some development teams better than the other, and even some applications better than others. A project dominated by DBAs is likely to favor a bottom-up design methodology, while one dominated by developers is somewhat more likely to favor a top-down design, depending on how comfortable they are with data design. If you are building a new application that will use an existing production database, you’re inherently taking the bottom-up approach because the database already exists.
Entity Framework supports both methodologies, although not at the same time in an application. It provides three ways to build an application, one that supports bottom-first design and two that support top-first design.
- Database-first design, available with the initial release of Entity Framework. This is the technique you used when you created the AdventureWorksLibrary project earlier in this chapter, based on objects in the existing AdventureWorksLT database. The Entity Data Model Wizard obtained the database schema and created the entity data objects for the tables you selected. The existing database schema shapes the entity objects using this technique, although you can modify the resulting objects extensively.
- Model-first design, introduced in version 4 of Entity Framework. If the database doesn’t yet exist, you can create an Entity Data Model using the designer in Visual Studio from scratch, or you can write the XML for the .edmx file directly, if you’re so inclined. When you’re ready, you can have Entity Framework create a new database that corresponds to the model you’ve designed, shaping the data and storage to the model. If you later make changes to the model, the designer in Visual Studio supports recreating the database with the changes.
- Code-first design, sometimes called code-only design. This is a new option released with version 4.1 of the Entity Framework. Here you write Plain Old CLR Objects (POCOs), which are regular .NET class objects, and Entity Framework will create the data store from these objects. Code-first works with an implied model at runtime, so there is no need for an .edmx model file. This technique is the purest form of top-down design, because you design your POCOs with no foresight about how the data will be persisted in a data store.