Use existing Connection object in DataContext

Discussion of open issues, suggestions and bugs regarding LinqConnect – Devart's LINQ to SQL compatible ORM
Post Reply
MiB1980
Posts: 3
Joined: Wed 19 Feb 2014 20:21

Use existing Connection object in DataContext

Post by MiB1980 » Wed 19 Feb 2014 20:27

My application needs to use a security dll. The security dll returns an OracleClient.OracleConnection object. I need to use this Connection object in my application. How do I make the DataContext use my Connection object?

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Use existing Connection object in DataContext

Post by MariiaI » Thu 20 Feb 2014 12:53

The necessary connection (IDbConnection) could be passed to the DataContext constructor:
http://www.devart.com/linqconnect/docs/ ... _ctor.html
In this case a new instance of the DataContext class is initialized by referencing the connection used by the .NET Framework.

MiB1980
Posts: 3
Joined: Wed 19 Feb 2014 20:21

Re: Use existing Connection object in DataContext

Post by MiB1980 » Thu 20 Feb 2014 14:56

I tried using the constructor of the DataContext as below:

<ProviderAttribute(GetType(Devart.Data.Oracle.Linq.Provider.OracleDataProvider))> _
Partial Public Class DataContext
Inherits Devart.Data.Linq.DataContext

Public Sub New()
MyBase.New(GetConnection(), GetMappingSource())
OnCreated()
End Sub

Private Shared Function GetConnection() As System.Data.IDbConnection
'Function has code to get the connection (OracleClient.OracleConnection object)
End Function
End Class

I get the following error in the call to MyBase.New:
A first chance exception of type 'System.ArgumentException' occurred in Devart.Data.Linq.dll
<Message>Invalid connection type. Expected Type: System.Data.OracleClient.OracleConnection, Actual Type: Devart.Data.Oracle.OracleConnection</Message>
<StackTrace> at Devart.Data.Linq.Provider.DataProvider.Initialize(Object connection)
at Devart.Data.Oracle.Linq.Provider.OracleDataProvider.Initialize(Object connection)
at Devart.Data.Linq.Provider.DataProvider.Devart.Data.Linq.Provider.IProvider.Initialize(IDataServices dataServices, Object connection)
at Devart.Data.Linq.DataContext.a(Object A_0, MappingSource A_1, Type A_2)
at Devart.Data.Linq.DataContext..ctor(IDbConnection connection, MappingSource mapping)

I am using the OracleDataProvider as the provider. Is this correct. Do I need to write a custom provider? Any other suggestions?

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Use existing Connection object in DataContext

Post by MariiaI » Fri 21 Feb 2014 13:43

I am using the OracleDataProvider as the provider. Is this correct. Do I need to write a custom provider? Any other suggestions?
Yes, to use a third-party data provider you need to write your custom data provider, which is inherited from the Devart.Data.Oracle.Linq.Provider.OracleDataProvider class.
JIC: We recommend you to use dotConnect for Oracle as a data provider when working with LinqConnect and Oracle. LinqConnect Professional Edition includes the Express editions of our data providers (dotConnect for Oracle bundled with LinqConnect provides basic connectivity features) and supports the corresponding databases. Please refer to: http://www.devart.com/linqconnect/editions.html

MiB1980
Posts: 3
Joined: Wed 19 Feb 2014 20:21

Re: Use existing Connection object in DataContext

Post by MiB1980 » Fri 21 Feb 2014 17:24

Is there any example of a custom provider inheriting from OracleDataProvider?

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Use existing Connection object in DataContext

Post by MariiaI » Mon 24 Feb 2014 16:06

We are sending you a test project to the e-mail address you have provided in your forum profile, that demonstrates the way to implement a custom data provider. Please check that the letter is not blocked by your mail filter.

Please tell us about the results.

Post Reply