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).

NewDev90
  • 379
  • 2
  • 21
  • Why you don't have a `Startup` class? What is the Visual Studio project type you are working on? – Vlad DX Nov 23 '20 at 11:46
  • In .NET Core, it works as you described. But the connection string should be in `appsettings.json` in the `ConnectionStrings` section. – Vlad DX Nov 23 '20 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). – NewDev90 Nov 23 '20 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. – Athanasios Kataras Nov 23 '20 at 11:52
  • The example can be found here: https://stackoverflow.com/questions/57699138/reading-connection-string-placed-in-asp-net-core-from-class-library-database-fi – Athanasios Kataras Nov 23 '20 at 11:52
  • @AthanasiosKataras I am not in disagreement, but I need to test my library functionality by itself - independently of whomever consumes it. In either case, even if provided, I can still not use the same simple code fragment as shown above in regular .Net? – NewDev90 Nov 23 '20 at 11:54
  • Of course you can. If you unit test, you can always provide the data by providing the options (similart way, as in .Net) `DbContextOptions options` – Athanasios Kataras Nov 23 '20 at 11:58
  • @AthanasiosKataras I should probably have added I am not familiar with .Net Core at all. But how would you parse along the options from a unit test to the dbcontext class as you explain it? I cannot visualize it for me I'm afraid. From .Net I have never provided these options explicitly, but instead just made use of `using(db = new dbcontext()...` directly and relying on the explicit connectionstring from appsettings. – NewDev90 Nov 23 '20 at 12:02
  • Take a look at the following [class project](https://github.com/karenpayneoregon/efcore5-getting-started/tree/master/ConfigurationHelper) for reading a .json file with parts to make up a connection string which is demonstrated in a [console project](https://github.com/karenpayneoregon/efcore5-getting-started/blob/master/ConnectionConfiguration/Context/SchoolContext.cs#L30), Both projects are .net core using ef core 5. Perhaps the code can be adapted to your task. – Karen Payne Nov 24 '20 at 02:53

0 Answers0