Why DataProvider is mandatory

Why DataProvider is mandatory

Postby ning » Tue 18 Dec 2012 07:20

I would like to create a datacontext with a specified Dbconnection, means a connection has already been created before. But i found that i was asked to provide a DataProvider through the DataProviderAttribute.

Why is this mandatory? If so, does that mean the datacontext internal will not alwasy use my connection but will use the DataProvider to create a new one?
ning
 
Posts: 8
Joined: Tue 18 Dec 2012 07:13

Re: Why DataProvider is mandatory

Postby MariiaI » Tue 18 Dec 2012 13:48

The point is that the Devart.Data.Linq.DataContext class can use different data providers to connect to different DBMSs. Thus, when a DataContext instance is created, it should know which data provider to use. You can specify this data provider, e.g., by setting the ProviderAttribute class attribute for YourDataContext.

In case, when the connection is passed to the DataContext constructor, exactly this connection will be used. For example:
Code: Select all
Devart.Data.Oracle.OracleConnection connection = new Devart.Data.Oracle.OracleConnection(connectionString);
YourDataContext context = new YourDataContext(connection);
For more information please refer to:
http://www.devart.com/linqconnect/docs/TheDataContext.html
http://www.devart.com/linqconnect/docs/Devart.Data.Linq~Devart.Data.Linq.DataContext~_ctor.html
Also, please pay attention to the MaxUsedConnections property, which gets or sets the maximal number of opened connections.
MariiaI
Devart Team
 
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Why DataProvider is mandatory

Postby ning » Thu 20 Dec 2012 01:51

Are you suggesting the dataconnection is still not enought for the datacontext, some other properties or methods are still required thought the dataprovider in order to make the datacontext work properly.
ning
 
Posts: 8
Joined: Tue 18 Dec 2012 07:13

Re: Why DataProvider is mandatory

Postby MariiaI » Thu 20 Dec 2012 12:46

You are right, DataContext cannot determine the DBMS it should work with based on the connection only, and needs a data provider description.
If you are encountering any difficulties with it, please specify them.

Feel free to contact us.
MariiaI
Devart Team
 
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Why DataProvider is mandatory

Postby ning » Sun 23 Dec 2012 05:18

ok, Let me ask my specific question.

I would like to wrap the oracleconnect with my own connection to replace the oralcecommand, and then insert some profiler codes before and after the oraclecommand.executequery or nonquery methods.

for example, i would like to use my connection called ProfiledOracleConnection(OracleConnection con)

However, due to some reason, I cannot use dbProviderFactory, since i want to keep using one connection i created at beginning.


Then i met the issue, the DataProvider.Initial method will expect the connection to be type OracleConnection, since i use a OracleDataProvider.

Even after i created my own DataProvider and override the CreateConnection method, i found another issue saying the codes cannot explicit convert ProfiledOracleConnection to OracleConnection.

I even tried to let ProfiledOracleConnection inherit from OracleConnection, some issues came in saying that the connection is not opened.

Could you help me find a way to replace the OracleCommand with my implementation which just insert several profile codes, and be able to stick to using one connection?

I know DBMonitor can help on profile, however that's still not meet my requirement.
ning
 
Posts: 8
Joined: Tue 18 Dec 2012 07:13

Re: Why DataProvider is mandatory

Postby MariiaI » Tue 25 Dec 2012 13:29

The DataProvider class checks the type of the connection passed to the context constructor. To avoid this, you can create child classes that are inherited from the OracleDataProvider and OracleConnection classes. Make sure that the ProviderAttribute of your DataContext class is set to the OracleDataProvider descendant.
We are sending you a sample project to the e-mail address you provided in your forum profile, please test it and tell us if this helps.
MariiaI
Devart Team
 
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Why DataProvider is mandatory

Postby ning » Thu 27 Dec 2012 01:50

Thanks very much. i already figured this out. You are right, beside this i also implement a custom dbproviderfactory and register it in the MyOracleProvider throught the provideName property.
ning
 
Posts: 8
Joined: Tue 18 Dec 2012 07:13


Return to LinqConnect (LINQ to SQL support)