expressappframework-402972-business-model-design-orm-business-model-design-with-entity-framework-core-use-the-entity-framework-core-data-model.md
This topic describes how to use the Entity Framework Core (EF Core) business model created within the DbContext entity container in XAF.
Read Tutorial: Define the Data Model and Set the Initial Data with EF Core
In Entity Framework Core, use DbContext to create and manage data.
File : MySolution.Module\BusinessObjects\MySolutionDbContext.cs
// ...
using DevExpress.ExpressApp.Design;
using DevExpress.ExpressApp.EFCore.DesignTime;
namespace MySolution.Module.BusinessObjects;
public class MySolutionDesignTimeDbContextFactory : DesignTimeDbContextFactory<MySolutionDbContext> {
protected override string ConnectionString
=> throw new InvalidOperationException("Connection string not specified. Specify connection string to your database here.");
// Specify the connection string used by the application and defined in the config file, either MySolution.Blazor.Server/appsettings.json or MySolution.Win/App.config
}
[TypesInfoInitializer(typeof(DbContextTypesInfoInitializer<MySolutionDbContext>))]
public class MySolutionDbContext : DbContext {
public MySolutionDbContext(DbContextOptions<MySolutionDbContext> 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);
// ...
}
}
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.
Important
XAF requires Multiple Active Result Sets (MARS) for EF Core applications connected to Microsoft SQL Server. Do not remove MultipleActiveResultSets=true; and do not set it to false in the connection string. For databases that do not support MARS, XAF has an internal mechanism to function without MARS. To enable it, include Persist Security Info=True in the connection string.
Specify the database connection in the ConnectionString section in the configuration file. For additional information, refer to the following topic: Specify EF Core Database Provider in XAF Application).
{
"ConnectionStrings": {
"ConnectionString": "Integrated Security=SSPI;Pooling=false;MultipleActiveResultSets=True;Data Source=(localdb)\\mssqllocaldb;Initial Catalog=MySolution",
// ...
},
// ...
}
<?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>
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.
Add the new entity to the solution’s DbContext in the MySolution.Module\BusinessObjects\MySolutionDbContext.cs file.
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 25.2\Components\XAF\MainDemo.NET.EFCore.
See Also