Winforms JSON Configuration files, DbContext in .NET Core
To make a Winform application to follow the guidelines of .NET Core principles
start by adding the following packages to the project.
We are making an example where we want to implement a known Entity Framework and DbContext and in order to do that we need to find a way to read from a configuration file, namely in JSON format from a well-known filename appsettings.json Here is the template for program.cs
We are making an example where we want to implement a known Entity Framework and DbContext and in order to do that we need to find a way to read from a configuration file, namely in JSON format from a well-known filename appsettings.json Here is the template for program.cs
using System;
using System.Windows.Forms;
using JustAPlainWinApp.Models;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
namespace JustAPlainWinApp
{
static class Program
{
private static ServiceCollection services;
private static IConfiguration config;
public static ServiceProvider { get; set; }
static void ConfigureServices()
{
//Add a reference to a configuration file (appsettings.json)
config = new ConfigurationBuilder() //From Microsoft.Extensions.Configuration
.AddJsonFile("appsettings.json")
.Build();
services = new ServiceCollection();
//An example of implementation/creation of a class (with an interface)
//services.AddTransient();
ServiceProvider = services.BuildServiceProvider();
//Add a reference to a DbContext class from Entity Framework
services.AddDbContext(options => options.UseSqlServer(config.GetConnectionString("FpcDb")));
}
[STAThread]
static void Main()
{
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Make a call to method above for configuring services
ConfigureServices();
//Collect the nescessary instantiated services to use in the Form class (Address the DbContext)
var buildsp = services.BuildServiceProvider();
var _fpc = buildsp.GetService();
Application.Run(new Form1(config, _fpc));
}
}
}
In the respective Form class you can use it in this way:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using JustAPlainWinApp.Models;
using Microsoft.Extensions.Configuration;
namespace JustAPlainWinApp
{
public partial class Form1 : Form
{
private IConfiguration config;
public FPCContext Fpc;
private List<FpcEmployee>> EmpList;
public Form1(IConfiguration configuration, FPCContext FpcContext)
{
InitializeComponent();
config = configuration;
Fpc = FpcContext;
}
private void Form1_Load(object sender, EventArgs e)
{
//Several ways to access the configuration items as shown just below
// var info1 = config.GetSection("Settings").GetSection("MyPrivateInfo").Value;
var num = config.GetValue("Settings:MyPrivateInfo");
//var x = config.GetValue(typeof(double), "Settings:MyPrivateInfo");
var info = config["Settings:MyPrivateInfo"];
label2.Text = $"MyPrivateInfo contains {info} and is of type " + num.GetType().FullName;
// Here access the content from the DbContext
EmpList = Fpc.FpcEmployees.ToList();
this.dataGridView1.DataSource = EmpList;
foreach (var emp in EmpList)
{
string[] itemArr = new string[]{ emp.Id.ToString(), emp.Name, emp.Alias, emp.Mobile,emp.HomeEmail};
listView1.Items.Add( new ListViewItem(itemArr));
}
dataGridView1.DataSource = EmpList;
dataGridView1.Columns["Id"].Visible = false;
dataGridView1.Columns["DepId"].Visible = false;
dataGridView1.Columns["JobStatus"].Visible = false;
dataGridView1.Columns["Valid"].ReadOnly = true;
dataGridView1.Columns["HomeEmail"].HeaderText = "E-mail";
}
}
}
Ensure to set Copy To Output Directory to Copy Always
So the outcome will possibly be like this: