Use the Entity Framework Core Data Model
- 3 minutes to read
This topic describes how to use the Entity Framework Core (EF Core) business model created within the DbContext entity container in XAF.
Specify the Entity Container (Context)
In Entity Framework Core, use DbContext
to create and manage data.
File: YourApplicationName.Module\BusinessObjects\YourApplicationNameDbContext.cs
// ...
using DevExpress.ExpressApp.Design;
using DevExpress.ExpressApp.EFCore.DesignTime;
namespace YourApplicationName.Module.BusinessObjects;
public class YourApplicationNameContextInitializer : DbContextTypesInfoInitializerBase {
protected override DbContext CreateDbContext() {
var optionsBuilder = new DbContextOptionsBuilder<YourApplicationNameEFCoreDbContext>()
.UseSqlServer(";")
.UseChangeTrackingProxies()
.UseObjectSpaceLinkProxies();
return new YourApplicationNameEFCoreDbContext(optionsBuilder.Options);
}
}
public class YourApplicationNameDesignTimeDbContextFactory : IDesignTimeDbContextFactory<YourApplicationNameEFCoreDbContext> {
public YourApplicationNameEFCoreDbContext CreateDbContext(string[] args) {
throw new InvalidOperationException("Make sure that the database connection string and connection provider are correct. After that, uncomment the code below and remove this exception.");
}
}
[TypesInfoInitializer(typeof(YourApplicationNameContextInitializer))]
public class YourApplicationNameEFCoreDbContext : DbContext {
public YourApplicationNameEFCoreDbContext(DbContextOptions<YourApplicationNameEFCoreDbContext> options) : base(options) { }
//public DbSet<ModuleInfo> ModulesInfo { get; set; }
public DbSet<ModelDifference> ModelDifferences { get; set; }
public DbSet<ModelDifferenceAspect> ModelDifferenceAspects { get; set; }
// ...
protected override void OnModelCreating(ModelBuilder modelBuilder) {
base.OnModelCreating(modelBuilder);
modelBuilder.HasChangeTrackingStrategy(ChangeTrackingStrategy.ChangingAndChangedNotificationsWithOriginalValues);
// ...
}
}
Important
XAF’s EF Core implementation does not support the following data model configurations: entities with composite/compound keys (multiple properties as an entity key), keyless entity types, and owned entity types. In such scenarios, inherit your EF Core entity from XAF’s BaseObject
class or define your own class with a single Guid, numeric (Int32, Int64 ), or String primary key. You can also use non-persistent objects to integrate XAF UI with legacy systems.
Specify the Database Connection
Important
XAF requires Multiple Active Result Sets in EF Core-based applications connected to a Microsoft SQL Server database. We do not recommend that you remove MultipleActiveResultSets=True;
from the connection string or set the MultipleActiveResultSets
parameter to false
. For other database management systems, we implement a mechanism internally that allows an application to correctly run without Multiple Active Result Sets.
ASP.NET Core Blazor
In ASP.NET Core Blazor applications, you can specify the database connection in the ConnectionString
section in the configuration file (MySolution.Blazor.Server\appsettings.json):
{
"ConnectionStrings": {
"ConnectionString": "Integrated Security=SSPI;Pooling=false;MultipleActiveResultSets=True;Data Source=(localdb)\\mssqllocaldb;Initial Catalog=MySolution",
// ...
},
// ...
}
Windows Forms
In Windows Forms applications, you can specify the database connection. For additional information, refer to the following topic: Connect an XAF Application to a Database Provider).
Use the ConnectionString
attribute in the configuration file (MySolution.Win\App.config).
```xml
<?xml version="1.0"?>
<configuration>
<!-- ... -->
<connectionStrings>
<add name="ConnectionString" connectionString="Integrated Security=SSPI;MultipleActiveResultSets=True;Data Source=(localdb)\mssqllocaldb;Initial Catalog=Solution111" providerName="System.Data.SqlClient" />
<!-- ... -->
</connectionStrings>
</configuration>
```
Add Entities to the UI
To display your entities in the Navigation System, follow the steps below.
Add the DefaultClassOptionsAttribute or NavigationItemAttribute to their implementations. To hide key properties from the UI, apply Browsable(false) attributes to them. You can apply other built-in attributes as well.
using System; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using DevExpress.Persistent.Base; using DevExpress.Persistent.BaseImpl.EF; // ... [DefaultClassOptions] public class Contact : BaseObject { public virtual String FullName { get; set; } } // Make sure that you use options.UseChangeTrackingProxies() in your DbContext settings.
Add the new entity to the solution’s DbContext in the MySolution.Module\BusinessObjects\MySolutionDbContext.cs file.
// ...
using Microsoft.EntityFrameworkCore;
namespace YourApplicationName.Module.BusinessObjects;
// ...
public class YourApplicationNameEFCoreDbContext : DbContext {
public YourApplicationNameEFCoreDbContext(DbContextOptions<YourApplicationNameEFCoreDbContext> options) : base(options) { }
// ...
public DbSet<Contact> Contacts { get; set; }
// ...
}
Related Demo
You can review the MainDemo.NET.EFCore demo application that ships with XAF. You can find the demo in the following folder: %PUBLIC%\Documents\DevExpress Demos 24.2\Components\XAF\MainDemo.NET.EFCore.