Page 2 of 2

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

Posted: Thu 02 Nov 2017 15:18
by Shalex
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.

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

Posted: Thu 02 Nov 2017 16:54
by azabluda
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();
        }
    }
}

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

Posted: Mon 06 Nov 2017 10:13
by azabluda
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

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

Posted: Mon 06 Nov 2017 12:19
by Shalex
Thank you for your report. We will investigate the issue and notify you about the result.

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

Posted: Mon 06 Nov 2017 13:01
by azabluda
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.

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

Posted: Tue 07 Nov 2017 14:16
by Shalex
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.

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

Posted: Fri 24 Nov 2017 11:48
by Shalex
New build of dotConnect for Oracle 9.5.399 is available for download: viewtopic.php?f=1&t=36257.