Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploying a WPF application that uses Entity Framework Core

Tags:

I want to build a WPF app that uses entity framework core so that I can handle SQLite databases.

I then do the following, for instance:

  • Using Visual Studio 2017 (15.5.2), create a WPF application.
  • Open the nuget package manager and install Microsoft.EntityFrameworkCore.Sqlite and Microsoft.EntityFrameworkCore.Tools.
  • Drop a button onto my MainWindow.xaml and add an event handler to the Click event such that the xaml looks like:
<Window x:Class="WPFEFCoreDeploymentTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Click="ButtonBase_OnClick"></Button>
    </Grid>
</Window>
  • Drop the following code onto the code behind file MainWindow.xaml.cs
namespace WPFEFCoreDeploymentTest
{
    using System.Linq;
    using System.Windows;
    using Microsoft.EntityFrameworkCore;

    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            using (var context = new SomeContext())
            {
                if (context.Database.GetPendingMigrations().Any())
                {
                    context.Database.Migrate();
                }
            }
        }
    }

    public class SomeContext : DbContext
    {
        public DbSet<Test> Tests { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite("Data Source=local.db");
        }
    }

    public class Test
    {
        public int Id { get; set; }
    }
}
  • Use package manager console to create the migration script: Add-Migration Migration1
  • Build and run the application

Now, if I go to the bin/Debug folder for my project, I notice that there are many DLLs that I assume to be the .NET core's implementation of the .NET standard (maybe I'm just being very stupid), for instance:

  • System.Collections.Immutable.dll
  • System.ComponentModel.Annotations.dll
  • System.Data.Common.dll
  • System.Net.Http.dll
  • the list goes on

So, if I understand correctly, when no redirects are added to the config app.config, these signed assemblies have to be accessible to the executable, meaning if I create an installer, I have to deploy these assemblies along with my application. But .NET framework already implement these, and .NET framework must be installed if someone wants to execute the WPF application on their machines. This makes me wonder:

  1. Can I replace these .net core implementation assemblies with .net framework's implementation? Is it possible?
  2. If the former point is technically possible, should it work or this may create all sorts of problems?
  3. If not possible, is it expected that mixing both .net framework and .net core will potentially lead to having duplicate implementations of assemblies referenced directly or indirectly by your application (maybe other modules of my application uses .net framework's System.Net.Http.dll classes but EF core or its dependencies references .net core's System.Net.Http.dll as well)?