Entity Framework Core DbContext set DbConnection

.net c# dbcontext entity-framework-core

Question

In .NET Framework was DbContext in EntityFramework 6 was constructor where I can set DbConnection.

In EF .NET Core can I set DbContext DbConnection? I hit an exception in UseTransaction of DbContext method:

An exception of type 'System.InvalidOperationException' occurred in Microsoft.EntityFrameworkCore.Relational.dll but was not handled in user code: 'The specified transaction is not associated with the current connection. Only transactions associated with the current connection may be used.'

Does any body know how set the DbConnection? Or I have get a wrong way?

1
0
2/12/2018 2:39:11 PM

Popular Answer

I have always added the Database Context within the startup file of .Net core apps by adding it to the IServiceCollection and pulling the string from the appsetting file like so:

        private void AddDatabase(IServiceCollection services)
    {
        var databaseConnectionString = Configuration.GetConnectionString("DatabseConnection");

        Check.CanConnectToDatabase(databaseConnectionString);

        services.AddDbContext<DatabaseContext>(
            options =>
            {
                options.ConfigureWarnings(
                    warnings => warnings.Throw(RelationalEventId.QueryClientEvaluationWarning));
                options.UseSqlServer(databaseConnectionString,
                    sqlOptions => sqlOptions.MigrationsHistoryTable(HistoryRepository.DefaultTableName, "SchemaName"));
            });
    }
0
2/12/2018 2:50:26 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow