Demonstrates how to use a class project to get connection strings for Entity Framework Core 5 in non-asp.net core projects e.g. WPF, Console and Windows Forms.
April 2022, see the following repository for EF Core 5 NuGet package to connect to SQL-Server with various options.
This is fine for hobbyist coder were the connection never changes while for professional development this method to connection to databases is restrictive in that when moving from development to test to production environment means the connection string must be updated and then rebuild the application for deployment.
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(
"Server=.\\SQLEXPRESS;" +
"Database=NorthWindAzureForInserts;Trusted_Connection=True;");
}
}
Another example for connection environments, production, test, development
{
"ConnectionStrings": {
"DevelopmentConnection": "Server=.\\SQLEXPRESS;Database=ocs;Integrated Security=true",
"ProductionConnection": "Server=.\\PROD;Database=ocs;Integrated Security=true",
"TestConnection": "Server=.\\TEST;Database=ocs;Integrated Security=true",
"Environment": 1
}
}
⚙️ The project visualBasicConnectionCodeSample (.NET Core 5) uses the C# connection helper.
- Microsoft Visual Studio 2019
- Microsoft SQL-Server (Express edition or better)
- Run schoolScript.sql to generate the database or configure appsettings.json for your database. Best to run against the included database first as it's been tested.
microsoft.extensions.configuration
microsoft.extensions.configuration.binder
microsoft.extensions.configuration.FileExensions
microsoft.extensions.configuration.Json
{
"database": {
"DatabaseServer": ".\\SQLEXPRESS",
"Catalog": "School",
"IntegratedSecurity": "true",
"UsingLogging": "true"
},
"ConnectionStrings": {
"DevelopmentConnection": "Server=.\\SQLEXPRESS;Database=School;Integrated Security=true",
"ProductionConnection": "Server=ProdServerDoesNotExists;Database=School;Integrated Security=true"
},
"Environment": {
"Production": false
}
}
{
"GeneralSettings": {
"LogExceptions": true,
"DatabaseSettings": {
"DatabaseServer": ".\\SQLEXPRESS",
"Catalog": "School",
"IntegratedSecurity": true,
"UsingLogging": true
},
"EmailSettings": {
"Host": "smtp.gmail.com",
"Port": 587,
"EnableSsl": true,
"DefaultCredentials": false,
"PickupDirectoryLocation": "MailDrop"
}
}
}
.NET Core desktop application configurations
Connectionstring.com for various connections string for different providers.
- C# Working with SQL-Server connection
- Entity Framework/Entity Framework Core dynamic connection strings (C#)
How to setup Entity Framework 6 and Entity Framework Core connections for desktop applications for different environments encrypted and unencrypted connection strings read from app.config
namespace EntityFramework6Library
{
using System.Data.Entity;
public partial class NorthWindContext : DbContext
{
#if Dev
public NorthWindContext() : base("name=DevConnection")
#elif Staging
public NorthWindContext() : base("name=StagingConnection")
#else
public NorthWindContext() : base("name=ProductionConnection")
#endif
{ }
public virtual DbSet<Contact> Contacts { get; set; }
public virtual DbSet<ContactType> ContactTypes { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
}
}
}