Web.config is like that.
<configSections>
<section name="configBuilders" type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false"/>
</configSections>
<connectionStrings configBuilders="Environment">
<add name="ConnectionString" connectionString="ConnectionString" providerName="System.Data.OracleClient" />
<add name="OracleDbContex" providerName="Oracle.ManagedDataAccess.Client" connectionString="OracleDbContex" />
</connectionStrings>
<configBuilders>
<builders>
<add name="Environment" mode="Greedy" type="Microsoft.Configuration.ConfigurationBuilders.EnvironmentConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Environment" />
</builders>
</configBuilders>
I have two connection string one of them is normal Ado.net connection. Another one is the oracle entity. But problem is that when program start and connection strings change from environment variables, ado.net connections work normally but dbcontex give this error.
System.InvalidOperationException: 'The connection string 'OracleDbContex' in the application's configuration file does not contain the required providerName attribute."'
If I don't use ConfigurationBuilders, two connection working normally. I need to get connection strings from environment variables. What should I do?
I found solution. Its about builder mode. I changed greedy to strict and it fixed.
When using Strict mode the configuration values are looked up first and every entry found is updated with override value (if present). Meaning the original connectionString XML node is kept with all its attributes, including the attribute providerName.
Related
I developed a Windows Service, using Entity Framework 6.
I run the service, and when it tries to connect to database, it throws the following exception:
No connection string named 'testEntities' could be found in the application config file.
so i decided to change this code in test.Context.cs
public testEntities()
: base("name=testEntities")
{
}
into this:
public testEntities()
: base("data source=MyLocalServer/sqlexpress;initial catalog=test;persist security info=True;user id=xxxxxx;password=xxxxxxx;MultipleActiveResultSets=True;App=EntityFramework")
{
}
in practice i changed the name of the entity to the connection string. But it throws the following exception:
The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly. To fix this problem do not remove the line of code that throws this exception.
If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection.
To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715
in the solution there are 2 projects (including wix for creating installation package). The same error is thrown if i set my project as "StartUp Project"..
Add the following sections to your app.config file.
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<connectionStrings>
<add name="testEntities" connectionString="metadata=res://*/Models.MyModel.csdl|res://*/Models.MyModel.ssdl|res://*/Models.MyModel.msl;provider=System.Data.SqlClient;provider connection string="data source=data source=MyLocalServer/sqlexpress;initial catalog=test;persist security info=True;user id=xxxxxx;password=xxxxxxx;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
/connectionStrings>
</configuration>
Where MyModel is the name of your database model.
Follow code
public testEntities()
: base("name=testEntities")
{
}
says that the connection string name in app.config is testEntities
Add connection string in app.config
<configuration>
...
<connectionStrings>
<add name="testEntities" connectionString="data source=MyLocalServer/sqlexpress;initial catalog=test;persist security info=True;user id=xxxxxx;password=xxxxxxx;" providerName="System.Data.EntityClient" />
...
I think it is better to add connection string to app.config
Add connectionstring in app.config
But If you want your solution . for get it to work
You should not set a default connection factory in app.config
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
<parameters>
<parameter value="System.Data.SqlServerCe.4.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
So You must remove defaultConnectionFactory.
Have you checked the App.Config or Web.Config files? It is where the connection string is defined to connect the project with the database, make sure when you create the Code First to locate your server where the database is, this will edit the file and generate inside the connection string tag, which you can edit as you wish.
I am trying to connect my C# application to an oracle database using Oracle.ManagedDataAccess. However, when I try to create the database it gives me the error below. Any ideas as to what I'm setting up incorrectly? I know that the provider name is set correctly, because I'm able to connect to the database exactly the same way with another C# application.
"The requested database ConnectionString.SomeName does not have a valid ADO.NET provider name set in the connection string"
On machine.config:
<add name="ConnectionString.SomeName" providerName="Oracle.ManagedDataAccess.Client" connectionString="Data Source=databaseSource;User Id=some_id;Password=some_password" />
On web.config:
<appSettings>
<add key="ConnectionString1" value="ConnectionString.SomeName"/>
</appSettings>
My code:
DatabaseProviderFactory factory = new DatabaseProviderFactory();
Database = factory.Create(ConfigurationManager.AppSettings["ConnectionString1"]);
In the Machine.config, check to ensure that you have two sections setup. One in " and another in . Examples below:
<appSettings>
<add key="ConnectionString.SomeName" value="Data Source=databaseSource;User Id=some_id;Password=some_password"/>
</appSettings>
<connectionStrings>
<add name="ConnectionString.SomeName" connectionString="Data Source=databaseSource;User Id=some_id;Password=some_password" providerName="Oracle.DataAccess.Client"/>
</connectionStrings>
The 'key' in your appSettings entry should match the 'name' in your connectionStrings entry.
For example, in your case, you should have this entry in appSettings:
<add key="ConnectionString.SomeName" value="Data Source=dataSource;User ID=some_id;Password=some_password" />
The solution that worked for me is to add the following to the App.config / Web.config (within the configuration tag):
<system.data>
<DbProviderFactories>
<remove invariant="Oracle.ManagedDataAccess.Client" />
<add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET, Managed Driver" type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
</DbProviderFactories>
</system.data>
I have been fighting for days with the new Google OAuth, and I believe I got it to work (removed and reinstalled owin), but now when I hit my site, I get:
SQLExpress database file auto-creation error:
...The connection string specifies a local Sql Server Express instance using a database location within the application's App_Data directory...
Indicating that it is trying to access a SQL Express db in the App_Data directory. I want it to connect to my existing external SQL Server
I have created a connection strings (two now).
I have tried to changing the connection string passed into the base of ApplicationDbContext to my connection string:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("myconnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
I tried both pointing to a straight connection string, and an entity string. The entity string originally errored out about tables not being included, but now it is giving me the same error.
I have tried changing the web.config to:
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="[tried a connection string here as well]"/>
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
I have read hints like a line in FilterConfig.cs (that references a function that doesn't seem to exist in EF 5):
filters.Add(new InitializeMembershipAttribute());
I am just getting started with EF 5 coming from ASP.NET - The basic concepts are no issue, the framework is great and flexible, but Membership issues have been a beyond frustrating experience (and tiring that I've been at it this long)! I have created small sites in MVC without membership before...
Are there specific web.config entries that are missing, similar to those from the old school ASP.NET membership?
I am also using [Authorize(Roles = "a,b,etc")] - do I need to specify something specific for roles, or should the change to the identity class take care of it as well?
You need to add the connection string to your web.config. The base name set in the DbContext must match the name in the web.config and IS case sensitive.
Remote db looks like first line and local db looks like the second line.
Like this:
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=0.0.0.0;Initial Catalog=DatabaseName;Persist Security Info=True;User ID=someuser;Password=somepass" providerName="System.Data.SqlClient" />
<add name="mycontext" connectionString="Data Source=(localdb)\v11.0; Initial Catalog=mycontext-20150116012312; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|mycontext-20150116012312.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>
... more content here ...
</configuration>
Hope this helps!
Let's say I have got simple WPF application using Entity Framework Code First to create database, connect to it and display some data. From start I do not want to worry about connection strings so after adding entityframework reference via Nuget I'll get auto generated app.config looking like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
I'll run test and observe connection string:
var strings = ConfigurationManager.ConnectionStrings;
with result:
[0] = {data source=.\SQLEXPRESS;Integrated Security=SSPI;attachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true}
As I Like to define my own connection string, I will add this into my app.config:
<connectionStrings>
<add name="MyContext" connectionString="data ource=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\myDb.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
And so when I run the test again and observe the connection srings I can see that there are two now:
[0] = {data source=.\SQLEXPRESS;Integrated Security=SSPI;attachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true}
[1] = {data source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\myDb.mdf;Integrated Security=True}
Why is it that I can see two connection string? If the first one is default, should it not be forgoten once I've created one?
Thanks
First connection string which you see comes from machine.config from your PC. It has following section:
<connectionStrings>
<add name="LocalSqlServer"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient"/>
</connectionStrings>
Which defines default connection string for ASP.NET database. If you really don't need it for your application, you can either edit machine.config file (not recommended) or clear connection strings before adding yours:
<connectionStrings>
<clear />
<add name="MyContext"
connectionString="data source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\myDb.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Also keep in mind - this connection string is not used by Entity Framework. By default it uses SQLEXPRESS server and database with name equal to full name of your DbContext class. You can check it by accessing context.Database.Connection.ConnectionString.
The configuration manager just grabs all of the connection strings defined in the app/web.config.
It can't make the generalized assumption that once you add a connection string you wouldn't want a default one around anymore. That second connection string might point to an entirely different database.
Using .net 3.5 and Enterprise library 5.0.
From my research, I found a similar issue here:
Activation error occured while trying to get instance of type ICacheManager, key "Cache Manager" *** this solution did not fix my issue.
I can't seem to figure it out, my config should be set up correctly, but keep getting the exception? Anyone have similar issues?
I made the suggestion to add cacheManager and reference when I call the cache manager:
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
.....
....
ICacheManager cm = CacheFactory.GetCacheManager("TrackingCacheManager");
The App.config:
<configuration>
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
<section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
<cachingConfiguration defaultCacheManager="TrackingCacheManager">
<cacheManagers>
<add name="TrackingCacheManager" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
expirationPollFrequencyInSeconds="120" maximumElementsInCacheBeforeScavenging="1000"
numberToRemoveWhenScavenging="10" backingStoreName="NullBackingStore" />
</cacheManagers>
<backingStores>
<add type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name="NullBackingStore" />
</backingStores>
</cachingConfiguration>
<dataConfiguration defaultDatabase="ConnectionString" />
<connectionStrings>
<add name="ConnectionString" connectionString="server=AFS7BCBRNGQ5O0\DEVELOPMENT;database=EITC_RTS;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
My references:
My guess is that you've setup this configuration in an App.config for your Domain project. But your main project has it's own config file which this configuration must be copied into.
So for example, if your main project was a webapp, then it would have a web.config. The caching configuration you have added to the App.config of the Domain project is not used at runtime. The configuration being used is from main project's config, in this example the web.config.
Copy your Caching configuration from the Domain App.Config to the main config file and it will work.
I have done silly mistake, but after read above, understand the issue. This is my vb.net code which give me the above error.
Imports System.Collections
'Imports System.Configuration
Public Class DatabaseLogic
Public ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("db").ToString()
Public Function ServerDataMagic(StoredProcedure As String, PDMdata As Hashtable) As DataSet
Dim db As Database = DatabaseFactory.CreateDatabase(ConnectionString ) 'Here I am getting error.
Using cmd As DbCommand = db.GetStoredProcCommand(StoredProcedure)
Try
db.DiscoverParameters(cmd)
Catch discover_ex As Exception
End Try
and in web.config entry is
<add name="db" connectionString="Database=Dbname;Server=SERVER;uid=sa;pwd=sa#1234" providerName="System.Data.SqlClient" />
After read just get the issue is CreateDatabase method wants a config entry key as a string and I was given the exact connection string via config entry access. This is my updated code.
Dim db As Database = DatabaseFactory.CreateDatabase("db") 'Here I changed the config entry key
I was post to somebody help.