0

I am trying to learn .Net core.

I have 2 to projects :

  1. .Net Core Web api project
  2. DLL .NetCore project contains my Unit of Work (entity framework)

My Questions:

  1. How I can pass connection string from appsettings.json to my DLL

    I have in startup.cs configuration but I don t how to use it to access to my appsettings.json

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
    

    there is services.AddDbContext in startup.cs

     
      services.AddDbContext(options => 
         options.UseSqlServer(
         Configuration.GetConnectionString("DefaultConnection")))

    but I don't want to use services.AddDbContext because is a couple to entity framework and if I have to change to other ORM I have to change this code also.

  2. other question how have a responsibility to decrypt the connection string.

    • a : webapi has to decrypt to the connection string and pass to DLL (unit of work)

    • b: or unit of work has to decrypt the connection string

  3. in the case I have another project (ie desktop application) how to use DLL (unit of work) do I have to put also the connection string inside my appsettings of the desktop application? (it like kind of repetition ? )

Lassi Uosukainen
  • 1,598
  • 13
  • 22
sam
  • 31
  • 5

3 Answers3

4

I don't want to use services.AddDbContext because is a couple of entity framework

So what you need to do is create an extension method in your EF project that wraps this call so the dependency to EF stays within the EF project like the following:

public static IServiceCollection AddDatabase<TContext>( this IServiceCollection serviceCollection, Action<DbContextOptionsBuilder> optionsAction, string connectionString)
{
    options.UseSqlServer(connectionString))
}

Then call this from your Startup Code:

services.AddDatabase(Configuration.GetConnectionString("DefaultConnection"));

That will resolve your dependency issue.

Regarding 2: see here: Encrypted Configuration in ASP.NET Core

Regarding 3: then the other projects will need to call the AddDatabase method and pass the connection string in, so the EF project will never have any knowledge about where to get it, it is always provided.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
Jamie Rees
  • 7,973
  • 2
  • 45
  • 83
0

You can do it like this:

public BooksContext(string connectionString): this(GetOptions(connectionString))
{ }

private static DbContextOptions GetOptions(string connectionString)
{
    return SqlServerDbContextOptionsExtensions.UseSqlServer(new DbContextOptionsBuilder(), connectionString).Options;
}
Ricardo Peres
  • 13,724
  • 5
  • 57
  • 74
0

You can get the in Stratup.cs file like this.

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;


private readonly IConfigurationRoot _appConfiguration;
private readonly IHostingEnvironment _hostingEnvironment;

public Startup(IHostingEnvironment env)
{
    _hostingEnvironment = env;
    _appConfiguration = env.GetAppConfiguration();
}

Code to get ConnectionString:

string connectionString = _appConfiguration["ConnectionStrings:Default"];

appsettings.json

{
  "ConnectionStrings": {
    "Default": "Server=YourDBAddress; Database=YourDB; Trusted_Connection=True;"
  }
  ...
}

For other points, you can refer @Jamie Rees's answer.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197