Skip to content

Commit 4256928

Browse files
Todor efcore snippets (#2176)
* move snippets to code base * minor region fix
1 parent 6a0e12c commit 4256928

6 files changed

Lines changed: 16 additions & 156 deletions

designing-reports/connecting-to-data/data-source-components/entitycoredatasource-component/configuring-the-database-connectivity-with-the-entitycoredatasource-component.md

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,46 +20,16 @@ A typical EF Core `DbContext` reads its connection string from `appsettings.json
2020

2121
To overcome this, the `EntityCoreDataSource` component exposes a `ConnectionString` property. When configuring the data source in the Web Report Designer or the Standalone Report Designer, the wizard prompts for the connection string and stores it in the report definition. At processing time, the component forwards the value to the `DbContext` constructor that accepts a `string` argument at processing time. The `DbContext` must therefore expose such a constructor, as shown in the following Code First sample:
2222

23-
```CSharp
24-
public class AppDbContext : DbContext
25-
{
26-
public AppDbContext()
27-
{
28-
}
29-
30-
public AppDbContext(string connectionString)
31-
: base(BuildOptions(connectionString))
32-
{
33-
}
34-
35-
private static DbContextOptions<AppDbContext> BuildOptions(string connectionString)
36-
{
37-
return new DbContextOptionsBuilder<AppDbContext>()
38-
.UseSqlServer(connectionString)
39-
.Options;
40-
}
41-
}
42-
```
23+
{{source=CodeSnippets\Blazor\Docs\DataSources\AppDbContext.cs region=CodeFirstAppDbContext}}
24+
4325

4426
## Setting the ConnectionString Property
4527

4628
To set the connection string in code, use either the property setter or the three-argument constructor:
4729

48-
```CSharp
49-
var dataSource = new Telerik.Reporting.EntityCoreDataSource
50-
{
51-
Context = typeof(AppDbContext),
52-
ContextMember = "People",
53-
ConnectionString = "Server=.;Database=MSSQLLocalDB;Integrated Security=True;TrustServerCertificate=True"
54-
};
55-
```
56-
57-
```CSharp
58-
var dataSource = new Telerik.Reporting.EntityCoreDataSource(
59-
"Server=.;Database=MSSQLLocalDB;Integrated Security=True;TrustServerCertificate=True",
60-
typeof(AppDbContext),
61-
"People");
62-
```
30+
{{source=CodeSnippets\Blazor\Docs\DataSources\AppDbContext.cs region=EFCoreConnectionStringThreeArgConstructor_0}}
31+
32+
{{source=CodeSnippets\Blazor\Docs\DataSources\AppDbContext.cs region=EFCoreConnectionStringThreeArgConstructor_1}}
6333

6434
The wizard stores the value as an attribute on the `EntityCoreDataSource` element of the `.trdp` or `.trdx` definition. To centralize the value and avoid embedding credentials in the file, use a named connection instead. Store it in the configuration file of the report-hosting process and read it at startup before assigning it to the `ConnectionString` property.
6535

designing-reports/connecting-to-data/data-source-components/entitycoredatasource-component/connecting-to-a-dbcontext-with-the-entitycoredatasource-component.md

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,11 @@ public EntityCoreDataSource(string connectionString, object context, string cont
3838

3939
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:
4040

41-
```CSharp
42-
var dataSource = new Telerik.Reporting.EntityCoreDataSource
43-
{
44-
Context = typeof(AppDbContext),
45-
ContextMember = "People"
46-
};
47-
```
41+
{{source=CodeSnippets\Blazor\Docs\DataSources\AppDbContext.cs region=EFCoreConnectionStringTwoArgConstructor_0}}
4842

4943
Use the three-argument constructor when you need to set the connection string, context type, and context member in a single call:
5044

51-
```CSharp
52-
var dataSource = new Telerik.Reporting.EntityCoreDataSource(
53-
"Server=.;Database=MSSQLLocalDB;Integrated Security=True;TrustServerCertificate=True",
54-
typeof(AppDbContext),
55-
"People");
56-
```
45+
{{source=CodeSnippets\Blazor\Docs\DataSources\AppDbContext.cs region=EFCoreConnectionStringThreeArgConstructor_1}}
5746

5847
## Binding to a DbContext Type Versus an Instance
5948

@@ -62,17 +51,7 @@ The `Context` property is typed as `object` and accepts either a `Type` referenc
6251
- 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](slug:entitycoredatasource-context-lifecycle) throughout report processing.
6352
- When you supply an **instance**, the application owns the lifetime of the context. The component does not call `Dispose` on the supplied instance.
6453

65-
```CSharp
66-
var context = new AppDbContext(connectionString);
67-
68-
var dataSource = new Telerik.Reporting.EntityCoreDataSource
69-
{
70-
Context = context,
71-
ContextMember = "People"
72-
};
73-
74-
// You have to dispose of the context manually after rendering the report.
75-
```
54+
{{source=CodeSnippets\Blazor\Docs\DataSources\AppDbContext.cs region=EFCoreConnectionStringTwoArgConstructor_1}}
7655

7756
## Code First Versus Database First
7857

designing-reports/connecting-to-data/data-source-components/entitycoredatasource-component/maintaining-the-lifecycle-of-the-context-with-the-entitycoredatasource-component.md

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,13 @@ When you assign a `Type` to the `Context` property (or pass one to the construct
3030
1. The instance is held for the duration of report processing so that lazy-loaded navigation properties remain available.
3131
1. The component disposes the instance when rendering completes.
3232

33-
```CSharp
34-
var dataSource = new Telerik.Reporting.EntityCoreDataSource
35-
{
36-
Context = typeof(AppDbContext),
37-
ContextMember = "People"
38-
};
39-
```
33+
{{source=CodeSnippets\Blazor\Docs\DataSources\AppDbContext.cs region=EFCoreConnectionStringTwoArgConstructor_0}}
4034

4135
## Supplying Your Own Instance
4236

4337
When you assign a live `DbContext` instance to the `Context` property, the application is responsible for disposing it. The component does not call `Dispose` on a context it did not create. Use this pattern when the context is wired into your application's dependency-injection container and must follow the container's scope.
4438

45-
```CSharp
46-
var context = serviceProvider.GetRequiredService<AppDbContext>();
47-
48-
var dataSource = new Telerik.Reporting.EntityCoreDataSource
49-
{
50-
Context = context,
51-
ContextMember = "People"
52-
};
53-
54-
// Use the data source while the context is alive, then dispose
55-
// the context explicitly when you no longer need the report:
56-
context.Dispose();
57-
```
39+
{{source=CodeSnippets\Blazor\Docs\DataSources\AppDbContext.cs region=EFCoreConnectionStringTwoArgConstructor_2}}
5840

5941
> warning Disposing the supplied `DbContext` before the report engine finishes processing the report breaks lazy loading and causes runtime exceptions. Coordinate the dispose call with the end of the report-processing pipeline.
6042

designing-reports/connecting-to-data/data-source-components/entitycoredatasource-component/overview.md

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -69,56 +69,11 @@ The following snippets show the minimal `DbContext` shape that the component can
6969

7070
* Code First scenario:
7171

72-
```CSharp
73-
public class AppDbContext : DbContext
74-
{
75-
public AppDbContext()
76-
{
77-
}
78-
79-
public AppDbContext(string connectionString)
80-
: base(BuildOptions(connectionString))
81-
{
82-
}
83-
84-
public DbSet<Person> People => Set<Person>();
85-
86-
private static DbContextOptions<AppDbContext> BuildOptions(string connectionString)
87-
{
88-
return new DbContextOptionsBuilder<AppDbContext>()
89-
.UseSqlServer(connectionString)
90-
.Options;
91-
}
92-
}
93-
```
72+
{{source=CodeSnippets\Blazor\Docs\DataSources\AppDbContextCodeFirst.cs region=CodeFirstAppDbContext}}
9473

9574
* Database First scenario:
9675

97-
```CSharp
98-
public class AppDbContext : DbContext
99-
{
100-
public AppDbContext()
101-
{
102-
}
103-
104-
public AppDbContext(DbContextOptions<AppDbContext> options)
105-
: base(options)
106-
{
107-
}
108-
109-
public DbSet<Person> People => Set<Person>();
110-
111-
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
112-
{
113-
if (!optionsBuilder.IsConfigured)
114-
{
115-
// Replace the literal below with your own connection string or
116-
// resolve it from configuration before calling UseSqlServer.
117-
optionsBuilder.UseSqlServer("<your connection string>");
118-
}
119-
}
120-
}
121-
```
76+
{{source=CodeSnippets\Blazor\Docs\DataSources\AppDbContextDbFirst.cs region=DbFirstAppDbContext}}
12277

12378

12479
## Supported Developer Platforms

designing-reports/connecting-to-data/data-source-components/entitycoredatasource-component/using-parameters-with-the-entitycoredatasource-component.md

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,17 @@ This article explains how to pass values from [Report Parameters](slug:telerikre
2020

2121
Add a method or queryable property on the `DbContext` that accepts the values you need to filter by. The names and types of the data source parameters must match the names and types of the corresponding method arguments exactly; otherwise the component throws an exception at runtime.
2222

23-
```CSharp
24-
public partial class AppDbContext : DbContext
25-
{
26-
public IQueryable<Person> QueryPeopleWithSalaryAbove(decimal threshold)
27-
{
28-
return this.Persons.Where(p => p.Salary > threshold);
29-
}
30-
}
31-
```
23+
{{source=CodeSnippets\Blazor\Docs\DataSources\AppDbContextParameters.cs region=AppDbContextParameters}}
3224

3325
## Mapping Report Parameters to the Data Source
3426

3527
Set the `ContextMember` property to the method name and add one entry to the `Parameters` collection of the data source for every method argument. The `Parameters` collection is inherited through `ObjectDataSourceBase` and exposes the same fluent overload used by every Telerik Reporting data source: `Add(name, type, valueOrExpression)`. Bind to a [Report Parameter](slug:telerikreporting/designing-reports/connecting-to-data/report-parameters/overview) by setting the value to an expression such as `=Parameters.threshold.Value`:
3628

37-
```CSharp
38-
var dataSource = new Telerik.Reporting.EntityCoreDataSource
39-
{
40-
Context = typeof(AppDbContext),
41-
ContextMember = "QueryPeopleWithSalaryAbove"
42-
};
43-
44-
dataSource.Parameters.Add("threshold", typeof(decimal), "= Parameters.threshold.Value");
45-
```
29+
{{source=CodeSnippets\Blazor\Docs\DataSources\AppDbContext.cs region=EFCoreConnectionStringTwoArgConstructor_3}}
4630

4731
You can also pass literal values when the data source must execute with a fixed argument:
4832

49-
```CSharp
50-
dataSource.Parameters.Add("threshold", typeof(decimal), 60000);
51-
```
33+
{{source=CodeSnippets\Blazor\Docs\DataSources\AppDbContext.cs region=EFCoreFixedParameter}}
5234

5335
## Pushing Filtering to the Server
5436

designing-reports/report-designer-tools/web-report-designer/tools/entitycoredatasource-wizard.md

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,7 @@ When started, the application that hosts the Web Report Designer will try to res
2525

2626
The default .NET application's configuration is the `appsettings.json` file:
2727

28-
```JSON
29-
"telerikReporting": {
30-
"assemblyReferences": [
31-
{
32-
"name": "EFCoreExample"
33-
}
34-
]
35-
}
36-
```
28+
{{source=CodeSnippets\Blazor\Docs\DataSources\efcore_appsettings.json region=RegisterEFCoreExample}}
3729

3830
Another option is a custom implementation of the [IConfiguration](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.configuration.iconfiguration) interface.
3931

0 commit comments

Comments
 (0)