Reputation: 2405
In program.cs i have created a configure services method
private static void ConfigureServices(IServiceCollection serviceCollection)
{
serviceCollection.AddSingleton(new LoggerFactory()
.AddSerilog());
serviceCollection.AddLogging();
serviceCollection.AddDbContext<Spiderdb>(options => options.UseSqlite("Data Source=(localdb)\\ProjectsV13;Initial Catalog=SpiderDB1;Integrated Security=True"));
// Initialize serilog logger
Log.Logger = new LoggerConfiguration()
.WriteTo.File("app.log")
.MinimumLevel.Debug()
.Enrich.FromLogContext()
.CreateLogger();
serviceCollection.AddTransient<App>();
}
This is how i'm using/ injecting
class App
{
private readonly ILogger<App> _logger;
private readonly Spiderdb _spiderDb;
public App(ILogger<App> logger, Spiderdb context)
{
_logger = logger;
_spiderDb = context;
}
public async void RunAsync()
{
_logger.LogInformation("Starting App");
using (_spiderDb)
{
var x = await _spiderDb.ProxyServersProviders.ToListAsync();
}
}
}
same connection string works fine if I don't inject and I use SQLServer instead of SQLlite, but not with DI. the classes for the models & context are built using EF power tools, but that's likely a red herring, is the connection string is not correct for SQL lite or have I not injected/registered correctly ?
Curiously the log mentions An 'IServiceProvider' was created for internal use by Entity Framework. which I'm sure I've seen somewhere & is to do with DI ?
2018-07-21 06:42:39.004 +01:00 [DBG] An 'IServiceProvider' was created for
internal use by Entity Framework.
2018-07-21 06:42:39.101 +01:00 [INF] Starting App
2018-07-21 06:42:40.160 +01:00 [INF] Entity Framework Core 2.1.1-rtm-30846
initialized 'Spiderdb' using provider
'Microsoft.EntityFrameworkCore.SqlServer' with options: None
2018-07-21 06:42:40.356 +01:00 [DBG] Compiling query model:
'from ProxyServersProvider <generated>_0 in DbSet<ProxyServersProvider>
select [<generated>_0]'
2018-07-21 06:42:40.413 +01:00 [DBG] Optimized query model:
'from ProxyServersProvider <generated>_0 in DbSet<ProxyServersProvider>
select [<generated>_0]'
2018-07-21 06:42:40.691 +01:00 [DBG] (QueryContext queryContext) =>
IAsyncEnumerable<ProxyServersProvider> _InterceptExceptions(
source: IAsyncEnumerable<ProxyServersProvider> _TrackEntities(
results: IAsyncEnumerable<ProxyServersProvider> _ShapedQuery(
queryContext: queryContext,
shaperCommandContext: SelectExpression:
SELECT [p].[Id], [p].[DailyReqCount], [p].[DailyReqLimit], [p].
[FailedCount], [p].[FirstReqToday], [p].[IsRestAPI], [p].[LastReq], [p].[URL]
FROM [ProxyServersProviders] AS [p],
shaper: UnbufferedEntityShaper<ProxyServersProvider>),
queryContext: queryContext,
entityTrackingInfos: { itemType: ProxyServersProvider },
entityAccessors: List<Func<ProxyServersProvider, object>>
{
Func<ProxyServersProvider, ProxyServersProvider>,
}
),
contextType: Spider.EF.Spiderdb,
logger: DiagnosticsLogger<Query>,
queryContext: queryContext)
2018-07-21 06:42:40.814 +01:00 [DBG] Opening connection to database
'SpiderDB1' on server '(localdb)\ProjectsV13'.
Upvotes: 0
Views: 1019
Reputation: 167
You can try using this format.
serviceCollection.AddDbContext<Spiderdb>(options => options.UseSqlite("Server=(localdb)\\ProjectsV13;Database=SpiderDB1;Trusted_Connection=True"));
Ref: https://learn.microsoft.com/en-us/ef/core/miscellaneous/connection-strings
Upvotes: 1