Home > .NET Core, C#, Entity Framework, WPF > Using Entity Framework Core with WPF on .NET Core 3.0

Using Entity Framework Core with WPF on .NET Core 3.0

We talked about how to configure a .NET Core 3.0 WPF application in the same way as we do with ASP.NET Core. We can keep using this approach also if we need to use Entity Framework Core. It is important to note that tipically we use an external service to perform data access (for example, a REST Web APIs backend), but in some cases (especially if we’re working with a legacy system) it might be necessary to access a database directly from within our application.

And the story is again the same: you use the same code of ASP.NET Core. Let’s see how to do that. As always, take this sample as reference. First of all, we need to add the NuGet package for Entity Framework Core:

Adding Entity Framework Core to .NET Core 3.0 WPF application

Adding Entity Framework Core to .NET Core 3.0 WPF application

In this example, we’re using Entity Framework with SQL Server, but of course we can use every database supported by this OR/M.

Now, let’s add a connection string in the appsettings.json file:

{
  "ConnectionStrings": {
    "SqlConnection": ""
  }
}

Then, we must create our data context, i.e., a class that inherits form DbContext and provides access to the database:

public class DataContext : DbContext
{
    // DbSet properties declarations...

    public DataContext(DbContextOptions<DataContext> options) : base(options)
    {
    }
}

The DataContext class contains a constructor that takes a DbContextOptions as argument. It is used to pass configuration settings to the Context via Dependency Injection. For the rest, we can add to the class the usual DbSets we are accustomed to and configure the model by overriding the OnConfiguring and OnModelCreating methods.

Finally, we need to register the DbContext in the ServiceProvider collection, like we did in the previous post:

private void ConfigureServices(IServiceCollection services)
{
     services.AddDbContext<DataContext>
        (options => options.UseSqlServer(
                    Configuration.GetConnectionString("SqlConnection")));

            
     services.AddTransient(typeof(MainWindow));
}

At lines 4-5 we setup the DataContext to use SQL Server with the connection string from the appsettings.json file we defined before.

Now we’re ready to use the DataContext in every Window we need it. For example:

private readonly DataContext dataContext;

public MainWindow(DataContext dataContext)
{
    InitializeComponent();

    this.dataContext = dataContext;
}

As we can see, also in this case the approach is the same we use with ASP.NET Core. Of course, we can choose to pass the DataContext to a business service, instead of accessing it directly: as long as the service is itself in the IoC container, everything works in the same way.

Advertisements
Categories: .NET Core, C#, Entity Framework, WPF
  1. No comments yet.
  1. 20/03/2019 at 16:47

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: