[Expand]General Information
[Collapse]WinForms Controls
 [Expand]What's Installed
 [Expand]Build an Application
 [Expand]Controls and Libraries
 [Collapse]Common Features
  [Expand]Data Binding Common Concepts
  [Expand]Data Source Wizard
  [Expand]Application Appearance and Skin Colors
  [Expand]Filtering UI Context
  [Expand]High DPI Support
  [Collapse]Scaffolding Wizard
   [Collapse]Data Access Layer
     How To: Generate Data Access Layer with Entity Framework Database First
     How To: Generate Data Access Layer with Entity Framework Code First
     How To: Generate Data Access Layer Based On Data Provided By a WCF Service
   [Expand]Generating an Application
  [Expand]Formatting Values
   HTML Text Formatting
  [Expand]Tooltip Management
  [Expand]Saving and Restoring Layouts to a File, Stream and System Registry
   Clipboard - Copy and Paste Operations. Data Formatting
   Version Compatibility: Default Property Values
  Get More Help
 [Expand]API Reference
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Report and Dashboard Server
[Expand]eXpressApp Framework
[Expand]eXpress Persistent Objects
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

How To: Generate Data Access Layer with Entity Framework Code First

This tutorial will guide you through the process of creating a code-first Data Access Layer. This layer will later be used by the Scaffolding Wizard to generate an application.

  1. In Visual Studio, go to "File | New | Project" or press CTRL+SHIFT+N to create a new project. Select the DevExpress Template Gallery option and choose the "Blank Application" template.
  2. Right-click your solution in the VS Solution Explorer window and click "Add | New Folder". Rename this new folder "Model".

  3. Select the newly created folder, then press "SHIFT+ALT+C" to add a new class named "Department". Repeat the process four times more to add the "Course", "Employee", "DepartmentContext" and "DepartmentContextInitializer" classes. The figure below illustrates the result in the VS Solution Explorer window.

  4. This data set will store entities of the "Department", "Course" and "Employee" classes in related data tables. Thus, populate each of these classes with the desired properties (data fields).

  5. The following steps involve using the Entity Framework DbContext, so add the related NuGet package to your project. To do this, go to "Project | Manage NuGet Packages..." in Visual Studio, then search for the "EntityFramework" package and install it.

  6. Derive your "DepartmentContext" class from DbContext and declare three properties of the System.Data.Entity.DbSet type. This class will represent a Data Access Layer and allow you to query and save data. DbSet properties, in turn, will represent tables that store records of related types.

  7. Declare a database initializer. This object will populate your database with sample data as the application starts for the first time.

    Note that in this code sample, your initializer is derived from the System.Data.Entity.DropCreateDatabaseIfModelChanges class. This initializer removes, re-creates and re-populates the database only if the model was changed since the last time the database was created. For this reason, your final application will not reflect any changes made to sample data in code after the first run. Moreover, any modifications made to your database at runtime will persist through subsequent application runs. If you want your application to start with clean sample data each time, derive the initializer from the DropCreateDatabaseAlways class instead.

  8. Finally, register your initializer on application start-up.

Expanded What's Next?

Congratulations, you have created a sample Data Access Layer with code-first Entity Framework! Check out other Data Access Layer tutorials or proceed to generating an application based on this layer.

Is this topic helpful?​​​​​​​