[Expand]General Information
[Expand]WinForms Controls
[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]Reporting
[Expand]Report and Dashboard Server
[Expand]Dashboard
[Collapse]eXpressApp Framework
 [Expand]Fundamentals
 [Expand]Getting Started
 [Expand]Concepts
 [Expand]Design-Time Features
 [Expand]Deployment
 [Collapse]Task-Based Help
  [Collapse]Business Model Design
   [Expand]Common Tasks
   [Collapse]Entity Framework (EF)
     How to: Apply Attributes to Entity Properties when Using Model First
     How to: Initialize Business Objects with Default Property Values in Entity Framework
     How to: Supply Initial Data for the Entity Framework Data Model
     How to: Use the Entity Framework Code First in XAF
     How to: Use the Entity Framework Data Model Located in an External Assembly
     How to: Use the Entity Framework Model First in XAF
   [Expand]eXpress Persistent Objects (XPO)
   [Expand]Non-Persistent Objects
  [Expand]Application Model
  [Expand]Actions
  [Expand]Navigation
  [Expand]Views
  [Expand]List Editors
  [Expand]Property Editors
  [Expand]Templates
  [Expand]Filtering
  [Expand]Reporting
  [Expand]Dashboards
  [Expand]Scheduler and Notifications
  [Expand]Maps
  [Expand]Security
  [Expand]Workflow
  [Expand]Localization
  [Expand]Testing
  [Expand]Miscellaneous UI Customizations
  Frequently Asked Questions (FAQ)
 [Expand]API Reference
[Expand]eXpress Persistent Objects
[Expand]CodeRush
[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: Use the Entity Framework Code First in XAF

This topic demonstrates how to create a simple XAF application with a business model in a DbContext context.

Note

  • This topic demonstrates the code that can be generated automatically by the Solution Wizard. Proceed, if you want to implement the demonstrated functionality in the existing XAF solution. If you are creating a new XAF solution, use the wizard instead.

  • A more complex example is provided in the EFDemoCodeFirst application that is shipped with XAF.

Tip

A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E4375.

Expanded 1. Create an XAF Application

Create a new XAF solution called MySolution using the DevExpress v18.2 XAF Solution Wizard. Select Entity Framework Code First at the Choose ORM step and click Finish.

Expanded 2. Add the Entity Data Model and Context

In this topic, we will not describe entities and context implementation in detail, as it is already described in MSDN (see Code First to a New Database). Here, we assume that you are already familiar with creating an EF data model in code.

  • In the module project, implement the following Employee and Task classes.

    In this code, you see the use of Data Annotations that influences UI generation - DefaultClassOptionsAttribute, ImageNameAttribute and FieldSizeAttribute, as well as the standard .NET Browsable attribute. As a result, the Employee and Task navigation items will be created, icons from the built-in image library will be used, the multiline editor will be displayed for the Task.Description property, and the service Id properties will be invisible in UI.

    Note

    You can use EF Power Tools to reverse engineer an existing database instead of writing code manually.

  • Implement the following DbContext descendant.

Note that the MyDBContext class should implement a constructor that takes the connectionString string parameter. This constructor will be called by the EFObjectSpaceProvider Object Space Provider internally.

Expanded 3. Use the Entity Framework Object Space Provider

To use the EFObjectSpace instances to access data in your application, modify the default implementation of the CreateDefaultObjectSpaceProvider method located in WinApplication.cs (WinApplication.vb) and WebApplication.cs (WebApplication.vb) files. For details on this code, refer to the Use the Entity Framework Data Model topic.

Expanded 4. Specify the Connection String to the Database

To connect the database that was generated for your data model, specify the XafApplication.ConnectionString property in code or use the corresponding option in the App.config and Web.config files located in the WinForms and ASP.NET application projects. Refer to the Connect an XAF Application to a Database Provider topic for details.

Expanded 5. Run the Application

Now you can run both the Windows Forms and ASP.NET applications. You will see that a UI is automatically generated for your data model.

Windows Forms:

ASP.NET:

Note

To learn how to fill the database with predefined data, refer to the How to: Supply Initial Data for the Entity Framework Data Model topic.

Expanded See Also

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