0

I am currently trying to set up a database connection with a .Net Core V3.1 library.

However, I cannot seem to figure out how I can parse my connection string into the dbcontext class. All the articles I have been able to find online, points to ASP.Net Core applications with a startup class. I do not have such a class.

In regular .Net I could simply do something like:

    public MyContext() : base("db.connection.mycontext")
    {
    }

    public DbSet<someClass> Objects { get; set; }

And it would fetch the correct file from my app.config file. How can I do something similar in .Net Core? Currently I am just setting up a local service-database from Visual Studio 2019, that I am intending to use for testing, and would like to have similar setup as shown above.

As a side note, I am using EFCore code-first to model the schemas (if this have any relevance).

9
  • Why you don't have a Startup class? What is the Visual Studio project type you are working on? Commented Nov 23, 2020 at 11:46
  • In .NET Core, it works as you described. But the connection string should be in appsettings.json in the ConnectionStrings section. Commented Nov 23, 2020 at 11:47
  • @VladimirSerykh I am working with a regular .Net Core class library. This does not contain a startup class as default. My question is how I can use it in the dbcontext class, similar to that of a simple .Net style (as I have shown in code above). Commented Nov 23, 2020 at 11:50
  • My guess is that you are making a .net core library. If that's the case, then you don't have a startup class, but the library itself, will not do anything on it's own. So you'll have the project that will consume it to provide it. Commented Nov 23, 2020 at 11:52
  • The example can be found here: stackoverflow.com/questions/57699138/… Commented Nov 23, 2020 at 11:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.