阅读ORMLite的文档,如果您使用的是IoC容器,它会将连接工厂注册为单例。
这是ASPNET Core 2.0的正确语法吗?或者我应该使用.addDBContext方法吗?
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);
答案 0 :(得分:0)
AddDbContext
用于Entity Framework Core。如果您不使用EF,请不要使用那个人。
我的方法如下:
services.AddSingleton(IDbConnectionFactory)
类中的Startup
。示例:https://github.com/afmorris/MorrisPhotos/blob/master/MorrisPhotos.Web/Startup.cs#L25 ServiceStackController
。示例:https://github.com/afmorris/MorrisPhotos/blob/master/MorrisPhotos.Web/Controllers/HomeController.cs#L15 IDbConnection
属性访问Db
。示例:https://github.com/afmorris/MorrisPhotos/blob/master/MorrisPhotos.Web/Controllers/HomeController.cs#L26