2

My project structure is pretty standard:

Project structure

within EFData.

EFData is an Entity Framework Core class library that isolates all of the database interaction. Database models, and my DBContext. I built it that way so that it's database environment agnostic.

The API project of course has a reference to EFData.

How do I pass the connection string from Startup.cs in API to DBContext.cs in EFData?

I've read multiple articles that reference a set up different than mine where I might expect a Startup.cs in my EFData project. I don't have that.

Within Startup.cs, I do have what I thought was the required line -

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

But EFData knows nothing about that connection string.

5
  • EFData doesn't need a startup file. If you place that AddDbContext call into your startup file within API, what value is coming back with that GetConnectionString call? Does your appsettings file have a connectionstring with that name? Commented Sep 4, 2019 at 22:30
  • The connection string returned is the one I'd expect based on my environment. Yes, the appsettings that corresponds to that environment has a connection with that name. @ShaneBauer Commented Sep 4, 2019 at 22:38
  • Without seeing how you intended to use the DBContext class, this is going to be hard to answer. How does your code use that context in the API project? Commented Sep 4, 2019 at 23:05
  • All of my database interaction is within that class library which I wanted to keep seperate from the API code Commented Sep 4, 2019 at 23:39
  • Does this answer your question? .Net Core passing connection string to DBContext class Commented Sep 26, 2021 at 12:29

2 Answers 2

3

One approach can be adding a ConnectionString property in the DBContext.cs class and then setting it on the startup.cs of the API project explicitly. Add the following code in DBcontext.cs

 using System; using System.Data.SqlClient; using Microsoft.EntityFrameworkCore; public class DBContext { public static void SetConnectionString(string connectionString) { if (ConnectionString == null) { ConnectionString = connectionString; } else { throw new Exception(); } } // this part will help you to open the connection public static SqlConnection OpenConnection() { SqlConnection connection = new SqlConnection(ConnectionString); connection.Open(); return connection; } private static string ConnectionString { get; set; } //add the connectionString to options protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) { optionsBuilder.UseSqlServer(ConnectionString); } } } 

Now, in the API project add a reference of the EFData project and in the startup.cs file set the ConnectionString

 public void Configure(IApplicationBuilder app, IHostingEnvironment env) { var connectionString = this.Configuration.GetConnectionString("DBName"); Namespace.DBContextContext.SetConnectionString(connectionString); //replace namespace with the namespace suitable for your solution //here goes rest of your default code } 

This way you should be able to access the Connection in your API project.

Sign up to request clarification or add additional context in comments.

Comments

0

Steps to Get Connection and build your database

  1. Add reference to EFData Project in API project - do this by in the API project, clicking on the dependecies section and adding reference to your EFData class library.
  2. Then add to your startup class in your api project and ensure the connection string is in your ConfigureServices method or similar

your connection string looks good just make sure your database connection string is correct.

Here is a link if you need more information

8 Comments

My EFData project does not have a startup.cs
Does your APi Project have one?
Yes, My API Project has a Startup.cs
So there are 3 things you need to do; 1. On your API project expand that section in Visual Studio (i assume from the screenshot) then right click on the item that says 'Dependencies', and then you will see the option to 'Add Reference', There you will see a list of projects in your solution and select EFData. Now your API project which is initialised on startup can initialize the EFData class library when you run the project.
2. Then you need to go into your startup class and in the ConfigureServices method add in the connection string you had and then you will need to reference the name space for your DBContext 3. Finall then you need to ensure the correct connection string is in your appsettings.json file by the name of 'DefaultConnection' so then it can be picked up on start up You may need to run your migrations and so on to build your database, but that should get you to the place you need
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.