Model as Code (Code First)

Developers often prefer to work with code, and the EF Code First model is a recognition of that fact. Code First allows you to write code that defines your model (tables and relationships). You can then easily work with your model to get data, update it, and save it back to the database.

Code First can be used to target an existing database or generate a database directly from the code model. Of course, you can also use the Visual Studio tools to generate a Code First model based on an existing database.

To write a Code First model in a given project, you start by adding appropriate references to Entity Framework or use the NuGet package manager to install the latest version of EF and add it to your project. Once your project is set up, you start by defining simple classes called plain old CLR objects (POCOs) that represent your database entities. You then create a database context object that uses EF to communicate to your database via your model classes. The following is an example.

Generate Code First from Existing Database

EF 6.1 introduced the ability to generate Code First from an existing database. To get started, you add a new ADO.NET Entity Data Model to your application by right-clicking your project and choosing Add, New Item and selecting the Data template group. This launches the Entity Data Model Wizard, as shown in Figure 1.15. Here you select Code First from Database to generate a Code First model based on existing database entities.

Image

FIGURE 1.15 The Entity Data Model Wizard simplifies the creation of Code First and Model First EF development.

The next step in the wizard is to define a connection string to your database. This connection string will be stored for you inside your application configuration file. The final step is to select the tables for which you want the wizard to generate Code First access. Figure 1.16 shows an example where Customers and Orders are selected from a database.

Image

FIGURE 1.16 Use the Entity Data Model Wizard to select tables to target for Code First development.

The Visual Studio Wizard then generates a database context class and simple classes to represent each of your database tables. The database context class (derived from System.Data.DbContext) defines objects (of type DbSet<TEntity>) that you use to work with your data. The DbContext classes knows how to get a connection to your database and read and write data. Figure 1.17 shows an example of this class in the code editor.

Image

FIGURE 1.17 The Code First DbContext object provides access to database tables as objects.

The simple POCO classes that the wizard generates contain properties that represent fields on your database. These classes have no dependency on EF. You use attributes to set rules for your data validation, such as required fields and maximum string lengths. Figure 1.18 shows the Customer class generated by the wizard.

Image

FIGURE 1.18 The wizard can generate your Code First POCO classes on your behalf.

The last step is to write code to read data into your Code First model, use it, and write data back to the database if required. Entity Framework makes this an easy process for developers. You access the data by creating a new instance of your DbContext. You then can create a LINQ query to access that data using your DbContext and DbSet collections. You can also add items to your DbSet collections and tell the context to save your changes. Figure 1.19 shows an example of querying the Customer table inside a console application.

Image

FIGURE 1.19 Use the DbContext object to access data from your database.


Note

Visual Studio, the .NET Framework, and even EF provide many additional ways to work with your data. This includes EF model first development, data synchronization, data sets, and others. Data access and development is covered in detail in Chapter 13, “Working with Databases.”


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset