Reputation: 1120
Reading the documentation for ORMLite, it says to register the Connection Factory as a singleton if you're using an IoC container.
Is this the correct syntax for that with ASPNET Core 2.0? Or should I be using the .addDBContext method?
var dbConnectString = Configuration["DBConnectString"];
var userName = Configuration["DBUserId"];
dbConnectString = dbConnectString.Replace("{your_username}", $"\'{userName}\'");
var password = Configuration["DBPassword"];
dbConnectString = dbConnectString.Replace("{your_password}", $"\'{password}\'");
var dbFactory = new OrmLiteConnectionFactory(dbConnectString,SqlServerDialect.Provider);
services.AddSingleton(dbFactory);
Upvotes: 0
Views: 493
Reputation: 1007
AddDbContext
is used for Entity Framework Core. Don't use that guy if you aren't using EF.
My method is as follows:
services.AddSingleton(IDbConnectionFactory)
in the Startup
class. Example: https://github.com/afmorris/MorrisPhotos/blob/master/MorrisPhotos.Web/Startup.cs#L25ServiceStackController
. Example: https://github.com/afmorris/MorrisPhotos/blob/master/MorrisPhotos.Web/Controllers/HomeController.cs#L15IDbConnection
via the Db
property in your controller. Example: https://github.com/afmorris/MorrisPhotos/blob/master/MorrisPhotos.Web/Controllers/HomeController.cs#L26Upvotes: 1