EF 5 supply connection string in code

EF 5 supply connection string in code

Postby travis_thelen » Thu 14 Mar 2013 15:46

I have a database first EF 5 model. I'd like to provide the connection string in code versus the config file.

I created a constructor as follows:
public Entities(string connectionstring) : base(connectionstring)

I passed in the connection string that was generated in my config file in code. I get an error: Keyword not supported: 'user id'.

Any assistance is appreciated.
travis_thelen
 
Posts: 17
Joined: Thu 14 Mar 2013 15:39

Re: EF 5 supply connection string in code

Postby Shalex » Thu 14 Mar 2013 17:14

Please specify:
1) the code generation template you are using (Tools > Entity Developer > Model Explorer > Templates)
2) your connection string (roughly, without credentials)

If you are generating a descendant of the ObjectContext class, refer to http://msdn.microsoft.com/en-us/library/bb739017.aspx.
Shalex
Devart Team
 
Posts: 7781
Joined: Thu 14 Aug 2008 12:44

Re: EF 5 supply connection string in code

Postby travis_thelen » Thu 14 Mar 2013 20:55

I am using the edmx model. Database first. It generates a DbContext.

Connection string is:

<connectionStrings>
<add name="Entities" connectionString="metadata=res://*/Mosaic.csdl|res://*/Mosaic.ssdl|res://*/Mosaic.msl;provider=Devart.Data.Oracle;provider connection string=&quot;user id=XXXXXXX;password=XXXXXXXXX;server=OracleStandard;direct=True;sid=ORCL;persist security info=True&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>
travis_thelen
 
Posts: 17
Joined: Thu 14 Mar 2013 15:39

Re: EF 5 supply connection string in code

Postby Shalex » Fri 15 Mar 2013 09:40

Try this code:
Code: Select all
...
using Devart.Data.Oracle;
using System.Data.EntityClient;
...
        OracleConnectionStringBuilder oracleBuilder = new OracleConnectionStringBuilder();
        oracleBuilder.UserId = "XXXXXXX";
        oracleBuilder.Password = "XXXXXXXXX";
        oracleBuilder.Server = "OracleStandard";
        oracleBuilder.Direct = true;
        oracleBuilder.Sid = "ORCL";
        oracleBuilder.PersistSecurityInfo = true;

        EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
        entityBuilder.Provider = "Devart.Data.Oracle";
        entityBuilder.ProviderConnectionString = oracleBuilder.ConnectionString;

        entityBuilder.Metadata = @"res://*/Mosaic.csdl|res://*/Mosaic.ssdl|res://*/Mosaic.msl";

        using (Entities context = new Entities(entityBuilder.ToString())) {
            var a = context.MyEntity.First();
        }

For more information, refer to
http://msdn.microsoft.com/en-us/library/bb738533.aspx
http://msdn.microsoft.com/en-us/library/cc716756.aspx
Shalex
Devart Team
 
Posts: 7781
Joined: Thu 14 Aug 2008 12:44

Re: EF 5 supply connection string in code

Postby travis_thelen » Fri 15 Mar 2013 16:29

Thanks. I am getting closer.

If I use your code, I get the following exception:

Schema specified is not valid. Errors:

Mosaic.ssdl(123,6) : error 0040: The Type CHAR is not qualified with a namespace or alias. Only primitive types can be used without qualification.

Mosaic.ssdl(124,6) : error 0040: The Type VARCHAR2 is not qualified with a namespace or alias. Only primitive types can be used without qualification.

Mosaic.ssdl(125,6) : error 0040: The Type VARCHAR2 is not qualified with a namespace or alias. Only primitive types can be used without qualification.

.....


Note that it works if I put the connection string in my web.config but construct the Entity with the code built connections string ... strange.

Thanks for your prompt help.

Regards,
Travis
travis_thelen
 
Posts: 17
Joined: Thu 14 Mar 2013 15:39

Re: EF 5 supply connection string in code

Postby travis_thelen » Fri 15 Mar 2013 17:12

Here is another piece of information. In the web.config connection string, I set the username and password to DONOTCONNECT (not a valid username/password) and the code worked when constructing the connection string via code.

Only missing info I can see is the web.config provides:

providerName="System.Data.EntityClient"
travis_thelen
 
Posts: 17
Joined: Thu 14 Mar 2013 15:39

Re: EF 5 supply connection string in code

Postby Shalex » Mon 18 Mar 2013 17:27

Please set "ColumnTypeCasingConventionCompatibility = false" before the first usage of the context (e.g.: in a static constructor of a partial class) or in *.config:
Code: Select all
<configuration>
  <configSections>
    <section name="Devart.Data.Oracle.Entity" type="Devart.Data.Oracle.Entity.Configuration.OracleEntityProviderConfigurationSection, Devart.Data.Oracle.Entity, Version=7.6.202.0, Culture=neutral, PublicKeyToken=09af7300eec23701" />
  </configSections>
  <Devart.Data.Oracle.Entity xmlns="http://devart.com/schemas/Devart.Data.Oracle.Entity/1.0">
    <CodeFirstOptions ColumnTypeCasingConventionCompatibility="false"/>
  </Devart.Data.Oracle.Entity>
</configuration>
Shalex
Devart Team
 
Posts: 7781
Joined: Thu 14 Aug 2008 12:44

Re: EF 5 supply connection string in code

Postby travis_thelen » Tue 19 Mar 2013 22:31

Ok, I tried setting ColumnTypeCasingConventionCompatibility in the code.

var config = Devart.Data.Oracle.Entity.Configuration.OracleEntityProviderConfig.Instance;
config.Workarounds.ColumnTypeCasingConventionCompatibility = true;

I needed to add a reference to Devart.Data.Oracle.Entity

only EF1, 4, and 6 were in the reference manager. So I browsed to the EF5 dll (Devart.Data.Oracle.Entity.dll).

All is fine until I try to compile ...

The type or namespace name 'Entity' does not exist in the namespace 'Devart.Data.Oracle' (are you missing an assembly reference?)
travis_thelen
 
Posts: 17
Joined: Thu 14 Mar 2013 15:39

Re: EF 5 supply connection string in code

Postby Shalex » Thu 21 Mar 2013 17:52

Please check the Target framework property of your project:
1) if it is .NET 4.0, add reference to \Program Files (x86)\Devart\dotConnect\Oracle\Entity\EF4\Devart.Data.Oracle.Entity.dll
2) if it is .NET 4.5, add reference to \Program Files (x86)\Devart\dotConnect\Oracle\Entity\EF5\Devart.Data.Oracle.Entity.dll
Shalex
Devart Team
 
Posts: 7781
Joined: Thu 14 Aug 2008 12:44


Return to dotConnect for Oracle