以下是appHost配置代码:
OrmLiteConfig.DialectProvider = PostgreSQLDialectProvider.Instance;
var dbFactory = new OrmLiteConnectionFactory();
dbFactory.RegisterConnection("NamedKeyConnOne", new OrmLiteConnectionFactory("ConnOne"))
{
ConnectionFilter = x => new ProfiledDbConnection(x, Profiler.Current)
});
dbFactory.RegisterConnection("NamedKeyConnTwo", new OrmLiteConnectionFactory("ConnTwo")
{
ConnectionFilter = x => new ProfiledDbConnection(x, Profiler.Current)
});
container.Register<IDbConnectionFactory>(dbFactory);
这是认证部分:
container.Register<IUserAuthRepository>(c => new OrmLiteAuthRepository(c.Resolve<IDbConnectionFactory>())); //Authentication and authorization
container.Resolve<IUserAuthRepository>().InitSchema();
所以我的问题是“如果没有默认的连接字符串,你如何传递正确的IDbConnectionFactory?”
谢谢你, 斯蒂芬
答案 0 :(得分:1)
您无法注入已命名的IDbConnection
连接,但可以从IDbConnectionFactory
解析该连接,您可以从以下服务中访问该连接:
public class MyServices : Service
{
public IDbConnectionFactory DbFactory { get; set; }
public object Any(Request request)
{
using (var db = DbFactory.OpenDbConnection("NamedKeyConnOne"))
{
//...
}
}
}