Connecting to a DbContext with the EntityCoreDataSource Component
This article describes how to connect the EntityCoreDataSource component to an Entity Framework Core DbContext for both the Code First and Database First workflows.
Configuring the Component in the Designer
Ensure your project fulfills the DbContext Requirements.
The simplest way to configure EntityCoreDataSource in either the Standalone Report Designer (.NET) or the Web Report Designer is through the wizard. The wizard starts automatically when you create a new Entity Framework Core Data Source from the toolbox, and you can launch it again later from the Configure option of the data source context menu.
The wizard walks you through the process as explained in:
- Standalone Report Designer for .NET EntityCoreDataSource Wizard
- Web Report Designer EntityCoreDataSource Wizard
After the wizard completes, the component appears in the Report Designer Data Explorer with the schema of the selected entity, and the report data items can bind to its fields.
Configuring the Component Programmatically
The EntityCoreDataSource class exposes three constructors:
public EntityCoreDataSource();
public EntityCoreDataSource(object context, string contextMember);
public EntityCoreDataSource(string connectionString, object context, string contextMember);
To create the simplest configuration, use the parameterless constructor and set the Context and ContextMember properties. Set Context to the DbContext type and ContextMember to the name of the DbSet<T>, queryable property, or method that retrieves the data:
var dataSource = new Telerik.Reporting.EntityCoreDataSource
{
Context = typeof(AppDbContext),
ContextMember = "People"
};
Use the three-argument constructor when you need to set the connection string, context type, and context member in a single call:
var dataSource = new Telerik.Reporting.EntityCoreDataSource(
"Server=.;Database=MSSQLLocalDB;Integrated Security=True;TrustServerCertificate=True",
typeof(AppDbContext),
"People");
Binding to a DbContext Type Versus an Instance
The Context property is typed as object and accepts either a Type reference or a live DbContext instance:
- When you supply a type, the component instantiates the
DbContext, holds it for the duration of report processing, and disposes it automatically. This is the recommended pattern because it preserves lazy loading throughout report processing. - When you supply an instance, the application owns the lifetime of the context. The component does not call
Disposeon the supplied instance.
var context = new AppDbContext(connectionString);
var dataSource = new Telerik.Reporting.EntityCoreDataSource
{
Context = context,
ContextMember = "People"
};
// You have to dispose of the context manually after rendering the report.
Code First Versus Database First
Both workflows are supported because the component only requires a DbContext derivative; it makes no assumption about how the model was authored:
- Code First. The
DbContextderives fromMicrosoft.EntityFrameworkCore.DbContextand configures the model inOnModelCreatingor through data annotations. Provide a constructor that accepts a connection string so that the report designer can pass the design-time connection string at runtime. - Database First. The
DbContextis generated byScaffold-DbContextagainst an existing database. The generated class typically already exposes a constructor that acceptsDbContextOptions<TContext>and one that accepts a connection string.