Last active
March 8, 2021 13:46
-
-
Save merken/3f5bd051672e36c591be61b413dd26c3 to your computer and use it in GitHub Desktop.
Providing a connection string in EF core
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"ConnectionStrings": { | |
"MyDb": "Data Source=127.0.0.1;Initial Catalog=mydb;persist security info=True;user id=sa;password=*****" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyDbContext : MyContext | |
{ | |
private readonly string connectionString; | |
public MyDbContext(string connectionString) : base() | |
{ | |
this.connectionString = connectionString; | |
} | |
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | |
{ | |
if (!optionsBuilder.IsConfigured) | |
{ | |
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings. | |
optionsBuilder.UseSqlServer(connectionString); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.IO; | |
using Microsoft.Extensions.Configuration; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var builder = new ConfigurationBuilder() | |
.SetBasePath(Directory.GetCurrentDirectory()) | |
.AddJsonFile("appsettings.json"); | |
var configuration = builder.Build(); | |
using (var db = new MyDbContext(configuration.GetConnectionString("MyDb"))) | |
{ | |
//Todo use db | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>netcoreapp2.0</TargetFramework> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0" /> | |
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" /> | |
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" /> | |
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" /> | |
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.0" /> | |
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="2.0.0" /> | |
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.0.0" /> | |
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" /> | |
</ItemGroup> | |
<ItemGroup> | |
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" /> | |
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" /> | |
</ItemGroup> | |
</Project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment