New to Telerik UI for WPF? Download free 30-day trial

Getting Started with WPF EntityFrameworkCoreDataSource

This article shows how to create a sample .NET Core 3.1 application using RadEntityFrameworkCoreDataSource and RadGridView.

RadEntityFrameworkCoreDataSource is built on top of Entity Framework Core. You should familiarize yourself with the framework before going over this tutorial.

Creating the WPF (.NET Core) Application

Start Visual Studio 2019 (or newer), select "Create a new project" and choose WPF App (.NET Core).

Figure 1: WPF (.NET Core) Application

WPF (.NET Core) Application

Adding References

Before we get started, we will add the following NuGet packages.

  • Microsoft.EntityFrameworkCore
  • Microsoft.EntityFrameworkCore.SqlServer
  • Microsoft.EntityFrameworkCore.Tools

    The required Microsoft.EntityFrameworkCore version varies based on the target .NET version of the Telerik dlls.

    • .NET Core—EFCore version 3.1.5
    • .NET 6—EFCore version 6.0.9
    • .NET 7—EFCore version 7.0.0

    Figure 2: NuGet Packages

    NuGet Packages

We will also add the needed Telerik references.

  • Telerik.Windows.Controls
  • Telerik.Windows.Controls.Data
  • Telerik.Windows.Controls.EntityFrameworkCore
  • Telerik.Windows.Controls.GridView
  • Telerik.Windows.Controls.Input
  • Telerik.Windows.Data

Xaml Usage

Example 1 demonstrates how you can set up the RadEntityFrameworkCoreDataSource and display data in a RadGridView. It assumes that you already have a DbContext named "MyEntityModel", which contains a DbSet called "Customers".

In case you don't have a DbContext/database setup, you can check out the MVVM Usage article, which shows how you can set them up from scratch.

Example 1: Defining RadEntityFrameworkCoreDataSource in xaml

<telerik:RadEntityFrameworkCoreDataSource Name="EntityFrameworkCoreDataSource" QueryName="Customers">  
    <telerik:RadEntityFrameworkCoreDataSource.DbContext>  
        <local:MyEntityModel/>  
    </telerik:RadEntityFrameworkCoreDataSource.DbContext>  
</telerik:RadEntityFrameworkCoreDataSource>  
<telerik:RadGridView ItemsSource="{Binding DataView, ElementName=EntityFrameworkCoreDataSource}"/> 

RelatedObjects

The RelatedObjects property of the RadEntityFrameworkCoreDataSource allows you to specify the names of the related entities that need to be retrieved. For example, if your main entity set is called "Customers", you might want to retrieve the "Orders" collection in case you want to get the related Orders for each Customer from the database. You can also specify several navigational property names separated by "/" characters and the control will use the ".ThenInclude()" method when retrieving them.

Example 2: Using the RelatedObjects

<telerik:RadEntityFrameworkCoreDataSource x:Name="coreDataSource" QueryName="Customers"> 
    <telerik:RadEntityFrameworkCoreDataSource.RelatedObjects> 
        <sys:String>Orders/OrderDetails</sys:String> 
    </telerik:RadEntityFrameworkCoreDataSource.RelatedObjects> 
    <telerik:RadEntityFrameworkCoreDataSource.DbContext> 
        <local:NorthwindContext /> 
    </telerik:RadEntityFrameworkCoreDataSource.DbContext> 
</telerik:RadEntityFrameworkCoreDataSource> 

See also

Telerik UI for WPF Learning Resources

In this article