EF 5 supply connection string in code
-
- Posts: 20
- Joined: Thu 14 Mar 2013 15:39
EF 5 supply connection string in code
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.
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.
Re: EF 5 supply connection string in code
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.
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.
-
- Posts: 20
- Joined: Thu 14 Mar 2013 15:39
Re: EF 5 supply connection string in code
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="user id=XXXXXXX;password=XXXXXXXXX;server=OracleStandard;direct=True;sid=ORCL;persist security info=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
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="user id=XXXXXXX;password=XXXXXXXXX;server=OracleStandard;direct=True;sid=ORCL;persist security info=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
Re: EF 5 supply connection string in code
Try this code:
For more information, refer to
http://msdn.microsoft.com/en-us/library/bb738533.aspx
http://msdn.microsoft.com/en-us/library/cc716756.aspx
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();
}
http://msdn.microsoft.com/en-us/library/bb738533.aspx
http://msdn.microsoft.com/en-us/library/cc716756.aspx
-
- Posts: 20
- Joined: Thu 14 Mar 2013 15:39
Re: EF 5 supply connection string in code
Thanks. I am getting closer.
If I use your code, I get the following exception:
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
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
-
- Posts: 20
- Joined: Thu 14 Mar 2013 15:39
Re: EF 5 supply connection string in code
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"
Only missing info I can see is the web.config provides:
providerName="System.Data.EntityClient"
Re: EF 5 supply connection string in code
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>
-
- Posts: 20
- Joined: Thu 14 Mar 2013 15:39
Re: EF 5 supply connection string in code
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?)
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?)
Re: EF 5 supply connection string in code
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
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