[EF.Core] How to create DbContext for existing DbConnection?

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for Oracle
Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: [EF.Core] How to create DbContext for existing DbConnection?

Post by Shalex » Thu 02 Nov 2017 15:18

You have encountered the exception with the following test, haven't you?

Code: Select all

        [TestMethod]
        public void DbContext_With_DbConnection_EnsureDeleted()
        {
            // Works fine with .UseOracle(ConnectionString)
            using (var dbContext = new TestDbContext(ConnectionString))
            {
                dbContext.Database.EnsureDeleted();
            }

            // Fails with .UseOracle(DbConnection)
            using (var dbConnection = new OracleConnection { ConnectionString = ConnectionString })
            {
                using (var dbContext = new TestDbContext(dbConnection))
                {
                    dbContext.Database.EnsureDeleted();
                }
            }
        }
We cannot reproduce the problem in our environment at the moment. Please run your code in the debug mode and make sure that the only Devart.Data.Oracle.Entity.EFCore.dll loaded in the process of your application is C:\Program Files (x86)\Devart\dotConnect\Oracle\Entity\EFCore2\Devart.Data.Oracle.Entity.EFCore.dll.

If this doesn't help, please upload a simple test project to some file exchange server.

azabluda
Posts: 35
Joined: Thu 10 Sep 2009 14:45

Re: [EF.Core] How to create DbContext for existing DbConnection?

Post by azabluda » Thu 02 Nov 2017 16:54

We configure DbContext a bit differently. We have one shared instance of serviceProvider and we pass it to different DbContextOptions/Builders via UseInternalServiceProvider. You can try this test.

Code: Select all

[TestMethod]
public void DbContext_With_DbConnection_EnsureDeleted2()
{
    var serviceCollection = new ServiceCollection();
    new OracleOptionsExtension().ApplyServices(serviceCollection);
    var serviceProvider = serviceCollection.BuildServiceProvider();

    using (var dbConnection = new OracleConnection { ConnectionString = ConnectionString })
    {
        var optionsBuilder = new DbContextOptionsBuilder();
        optionsBuilder.UseInternalServiceProvider(serviceProvider);
        optionsBuilder.UseOracle(dbConnection);

        using (var dbContext = new DbContext(optionsBuilder.Options))
        {
            dbContext.Database.EnsureDeleted();
        }
    }
}

azabluda
Posts: 35
Joined: Thu 10 Sep 2009 14:45

Re: [EF.Core] How to create DbContext for existing DbConnection?

Post by azabluda » Mon 06 Nov 2017 10:13

I also checked it with the shorter DbContext configuration method, still fails with InvalidCastException.

Code: Select all

[TestMethod]
public void DbContext_With_DbConnection_EnsureDeleted3()
{
    using (var dbConnection = new OracleConnection { ConnectionString = ConnectionString })
    {
        var optionsBuilder = new DbContextOptionsBuilder();
        optionsBuilder.UseOracle(dbConnection);

        using (var dbContext = new DbContext(optionsBuilder.Options))
        {
            dbContext.Database.EnsureDeleted();
        }
    }
}
I pushed both tests to my repo https://github.com/azabluda/dotConnectOracle

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: [EF.Core] How to create DbContext for existing DbConnection?

Post by Shalex » Mon 06 Nov 2017 12:19

Thank you for your report. We will investigate the issue and notify you about the result.

azabluda
Posts: 35
Joined: Thu 10 Sep 2009 14:45

Re: [EF.Core] How to create DbContext for existing DbConnection?

Post by azabluda » Mon 06 Nov 2017 13:01

You may find it helpful as well, but the example you mentioned in viewtopic.php?f=1&t=34033&p=126053#p126012 only seems to work thanks to

Code: Select all

.UseOracle(ConnectionString)
preceding the actual test of

Code: Select all

.UseOracle(DbConnection)
which hints to some global side effects inside the provider. If you only run the second block then it fails too.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: [EF.Core] How to create DbContext for existing DbConnection?

Post by Shalex » Tue 07 Nov 2017 14:16

The bug with using UseOracle(DbConnection) extension method of DbContextOptionsBuilder in EF Core is fixed. We will notify you when the corresponding build of dotConnect for Oracle is available for download.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: [EF.Core] How to create DbContext for existing DbConnection?

Post by Shalex » Fri 24 Nov 2017 11:48

New build of dotConnect for Oracle 9.5.399 is available for download: viewtopic.php?f=1&t=36257.

Post Reply