NOTE: We encourage our Entity Framework 6 (EF 6) users to consider Entity Framework Core (EF Core) for new XAF's Blazor and WinForms .NET Core projects. Microsoft has moved EF 6 into maintenance mode, and as such, EF 6 will not mirror XAF’s .NET 5+ offering. At present, EF Core supports key XAF technologies/capabilities including advanced security and comprehensive audit trail support. EF Core also offers better performance when compared to EF 6. For more information, see Porting from EF 6 to EF Core | XAF Business Model Design with Entity Framework Core.
With the v16.2 release, the Security System provides the new navigation permission system in XAF applications. If your application uses Entity Framework as the ORM system and the Permission Policies security system, you need to perform a migration. It allows you to avoid the database version error and use the new functionality.
After your application is updated using the DevExpress Project Converter, the following error can occur at the application startup:
The model backing the 'MySolutionDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).
This occurs because the current database structure does not suit the updated application. To prevent this error, perform a migration with the following steps.
1. Create the migration configuration
Add a new class to the module project and specify its name. Replace the code in the new class with the code below.
C#using System;
using System.Data.Entity.Migrations;
using MySolution.Module.BusinessObjects;
//…
public partial class MigrationConfiguration : DbMigrationsConfiguration<MySolutionDbContext>
{
public MigrationConfiguration() {
AutomaticMigrationsEnabled = true;
ContextKey = "MySolution.Module.BusinessObjects.MySolutionDbContext";
}
}
Visual BasicImports System
Imports System.Data.Entity.Migrations
Imports MySolution.Module.BusinessObjects
‘…
Partial Public Class MigrationConfiguration
Inherits DbMigrationsConfiguration(Of MySolutionDbContext)
Public Sub New()
AutomaticMigrationsEnabled = True
ContextKey = "MySolution.Module.BusinessObjects.MySolutionDbContext"
End Sub
End Class
In the code above, the MigrationConfiguration configuration was created for the context used in your project. Set the AutomaticMigrationsEnabled property to true to automatically apply the migration at the first application startup. The ContextKey property should specify a full path to your DbContext file in the current project.
2. Set the initializer
At the next step, the MigrateDatabaseToLatestVersion database initializer is registered.WinForms
Open the Program.cs (Program.vb) file and add the following code there.
C#using System.Data.Entity;
using MySolution.Module;
using MySolution.Module.BusinessObjects;
static class Program {
static void Main() {
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MySolutionDbContext, MigrationConfiguration>());
//...
}
//...
}
Visual BasicImports System.Data.Entity
Imports MySolution.Module
Imports MySolution.Module.BusinessObjects
Public Class Program
Public Shared Sub Main(ByVal arguments() As String)
Database.SetInitializer(New MigrateDatabaseToLatestVersion(Of MySolutionDbContext, MigrationConfiguration)())
'...
End Sub
'...
End Class
ASP.NET
Open the Global.asax.cs (Global.asax.vb) file and add the following code there.
C#using System.Data.Entity;
using MySolution.Module;
using MySolution.Module.BusinessObjects;
public class Global : System.Web.HttpApplication {
protected void Application_Start(Object sender, EventArgs e) {
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MySolutionDbContext, MigrationConfiguration>());
//...
}
//...
}
Visual BasicImports System.Data.Entity
Imports MySolution.Module
Imports MySolution.Module.BusinessObjects
Public Class [Global]
Inherits System.Web.HttpApplication
Protected Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
Database.SetInitializer(New MigrateDatabaseToLatestVersion(Of MySolutionDbContext, MigrationConfiguration)())
'...
End Sub
'...
End Class
After the initializer is set, the target database will be updated using the related migration configuration if the database state is not actual.
To learn more, refer to the Code First Migrations MSDN topic.