What is this Error.
System.ArgumentException: 'Keyword not supported: 'metadata'.'
Line of follow is my app.config.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="Foroush.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
<connectionStrings>
<add name="ForoushEntities" connectionString="metadata=res://*/DataModel.csdl|res://*/DataModel.ssdl|res://*/DataModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=Foroush;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient"/>
</connectionStrings>
<applicationSettings>
<Foroush.Properties.Settings>
<setting name="Foroush_SmsWebService_SendReceive" serializeAs="String">
<value>http://ip.sms.ir/ws/SendReceive.asmx</value>
</setting>
</Foroush.Properties.Settings>
</applicationSettings>
</configuration>
For Example
In the screenshot I see you alter the connection string by replacing an important part with "" in an attempt to create a Sql Connection String instead of an Entity Framework Connection String.
There are build-in methods for that like:
var entityConnectionStringBuilder = new EntityConnectionStringBuilder(MyConnectionString);
string sqlConnectionString = entityConnectionStringBuilder.ProviderConnectionString;
return new System.Data.SqlClient.SqlConnectionStringBuilder(sqlConnectionString).UserId);
In the screenshot code you probably wanted to use ConnectionStringNewFormat as an argument in the SqlConnectionStringBuilder instead of MyConnectionString (which is the original Entity Framework Connection String. But please, do not use string manipulation but use the code provided to you by the Entity Framework as demonstrated above.
Related
I'm trying to retrieve data from both MySQL and SQL Server on the same console application. I manage to retrieve data from MySQL, however when I trying to retrieve data from SQL Server, I got System.ArgumentException: 'Option not supported. Parameter name: multipleactiveresultsets' error.
The following is my app.config:
<?xml version="1.0" encoding="utf-8" ?>
<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>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<connectionStrings>
<add name="MySQLDb" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;port=3306;database=sakila;uid=some_user;password=some_password"/>
<add name="SQLDb" providerName="System.Data.SqlClient" connectionString="data source=USER-PC\SQLEXPRESS;initial catalog=MyDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"/>
</connectionStrings>
<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"/>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
</entityFramework>
</configuration>
And my C#:
#region MySQL.
{
var dbContext = new MySQLDb();
var dbSet = dbContext.Set<Actor>();
var actors = dbSet.ToList();
}
#endregion
#region SQLServer.
{
var dbContext = new SQLDb();
var dbSet = dbContext.Set<User>();
var users = dbSet.ToList(); // <-- Throw exception.
}
#endregion
If I disable entityFramework section in app.config and MySQL code block in my C# code, I can retrieve data from SQL Server without any issue.
Version info
MySQL.Data.Entity 6.10.8
NET Framework 4.6.1
Any idea?
Update 1
Found out that the connection type for MySQLDb is MySql.Data.MySqlClient.MySqlConnection, so that works just fine. But when instantiating SQLDb, the connection type is still MySql.Data.MySqlClient.MySqlConnection instead of System.Data.SqlClient.SqlConnection. How should we fix this?
The issue is that we are using MySql.Data.Entity.MySqlEFConfiguration (in the app.config) which set the default connection factory to use MySqlConnectionFactory.
The solution is to use a custom DbConfiguration in place of MySql.Data.Entity.MySqlEFConfiguration to deter from setting the default connection factory.
public class MySQLDbConfiguration : DbConfiguration
{
public MySQLDbConfiguration()
{
SetProviderServices(MySqlProviderInvariantName.ProviderName, new MySqlProviderServices());
SetProviderFactory(MySqlProviderInvariantName.ProviderName, new MySqlClientFactory());
}
}
Declare the instance as readonly somewhere in the code,
private static readonly MySQLDbConfiguration DBConfig = new MySQLDbConfiguration();
and set the configuration PRIOR TO using any EF features
DbConfiguration.SetConfiguration(DBConfig);
And our app.config now becomes
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<connectionStrings>
<add name="MySQLDb" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;port=3306;database=sakila;uid=some_user;password=some_password"/>
<add name="SQLDb" providerName="System.Data.SqlClient" connectionString="data source=USER-PC\SQLEXPRESS;initial catalog=MyDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"/>
</connectionStrings>
</configuration>
If you're opt to use app.config instead of deriving a custom DbConfiguration, you can do the following
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- Alternative to custom DbConfiguration. -->
<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>
<providers>
<provider invariantName = "MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"/>
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant = "MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.10.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/>
</DbProviderFactories>
</system.data>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<connectionStrings>
<add name="MySQLDb" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;port=3306;database=sakila;uid=some_user;password=some_password"/>
<add name="SQLDb" providerName="System.Data.SqlClient" connectionString="data source=USER-PC\SQLEXPRESS;initial catalog=MyDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"/>
</connectionStrings>
</configuration>
If you are using asp net 5.0, install the Nuget found here, but first uninstall Mysql.data.
I don't even know if I can do what I'm attempting but I've imported forms from several projects and added references to those projects. Each project has a different set of connection strings and I'm trying to get them to coexist in App.config where I can filter by SECTION (Users select connections from comboboxes). I am hoping I can do this by implementing ConfigSections. If it's doable I obviously don't know how.
Attached is my App.config. I'm getting the error 'configuration system failed to initialize' and when I drill into the detail it says 'unrecognized configuration section amSettings
Is what I'm trying to do possible? If so, what do I need to correct?
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="ApplicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="amSettings.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
allowExeDefinition="MachineToLocalUser"
requirePermission="false"/>
<section name="cbSettings.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false"/>
</sectionGroup>
</configSections>
<amSettings>
<add key="VX130 Attribute Map Connections" value="Sample Console Application" />
<add key="Region 1 VX130" value="Server=R01SCRDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 2 VX130" value="Server=R02LITDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 3 VX130" value="Server=R03DURDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 4 VX130" value="Server=R04PHIDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="CDW" value="Server=VHACDWA01;Database=;Trusted_Connection=true;"/>
</amSettings>
<cbSettings>
<add key="CDW Class Builder Connections" value="Sample Console Application" />
<add key="Region 1 Class Build" value="Server=R01SCRDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 2 Class Build" value="Server=R02LITDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 3 Class Build" value="Server=R03DURDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 4 Class Build" value="Server=R04PHIDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="DEVELOPMENT Class Build" value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
<add key="PREVIEW Class Build" value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
<add key="VERSION Class Build" value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
</cbSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
Change the section name from amSettings.Properties.Settings to amSettings and cbSettings.Properties.Settings to cbSettings
e.g.
`<section name="amSettings" `
Here is a comprehensive example:
If you change your config file to this:
<configSections>
<section name="amSettings"
type="System.Configuration.AppSettingsSection"
allowExeDefinition="MachineToLocalUser"
requirePermission="false"/>
<section name="cbSettings"
type="System.Configuration.AppSettingsSection"
requirePermission="false"/>
</configSections>
<amSettings>
<add key="ABC" value="DEF"/>
</amSettings>
Then you can access the key ABC using this code:
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection appSettingSection = (AppSettingsSection)config.GetSection("amSettings");
var a = appSettingSection.Settings["ABC"].Value;
The solution was two things. Change section name as user469104 recommended and wrapping sections in Group Name.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="ApplicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="amSettings"
type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
allowExeDefinition="MachineToLocalUser"
requirePermission="false"/>
<section name="cbSettings"
type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false"/>
</sectionGroup>
</configSections>
<ApplicationSettings>
<amSettings>
<add key="VX130 Attribute Map Connections" value="Sample Console Application" />
<add key="Region 1 VX130" value="Server=R01SCRDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 2 VX130" value="Server=R02LITDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 3 VX130" value="Server=R03DURDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 4 VX130" value="Server=R04PHIDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="CDW" value="Server=VHACDWA01;Database=;Trusted_Connection=true;"/>
</amSettings>
<cbSettings>
<add key="CDW Class Builder Connections" value="Sample Console Application" />
<add key="Region 1 Class Build" value="Server=R01SCRDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 2 Class Build" value="Server=R02LITDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 3 Class Build" value="Server=R03DURDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="Region 4 Class Build" value="Server=R04PHIDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
<add key="DEVELOPMENT Class Build" value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
<add key="PREVIEW Class Build" value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
<add key="VERSION Class Build" value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
</cbSettings>
</ApplicationSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
I'm getting this exception when using the code:
var appSettings = ConfigurationManager.AppSettings; //Exception here
string result = appSettings["ILFSsqlServer"] ?? "Not Found"; //trying to get to this point
I'm not really sure why and from previous answers to this question I think I'm doing exactly what they suggest but to no avail.
My app.config is:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="userSettings"
type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
</sectionGroup>
</configSections>
<AppSettings>
<add key="ILFSsqlServer" value="ODBC;DSN=sql server copycloas;Trusted_Connection=Yes;APP=Microsoft Office 2010;DATABASE=ILFSView;"/>
</AppSettings>
</configuration>
On your app.config you should write appSettings, lowercase A.
what I have in app.config is this
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="Porject.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
<applicationSettings>
<Porject.Properties.Settings>
<setting name="PhotoLocation" serializeAs="String">
<value>.\photos\</value>
</setting>
</Porject.Properties.Settings>
</applicationSettings>
<connectionStrings>
<add name="****" connectionString="Data Source=***;Initial Catalog=****;User ID=***;Password=***" />
</connectionStrings>
</configuration>
and this is how I call the PhotoLocation:
string s = ConfigurationManager.AppSettings["PhotoLocation"];
When I make a request to get the connectionstring, there is no problem and it works fine; but when I requst the PhotoLocation it returns null.
Also the ConfigurationManager.AppSettings.Count returns 0.
Does anyone have an idea on what I'm doing wrong?
simply add this in your App config file
<appSettings>
<add key="PhotoLocation" value=".\photos\"/>
<appSettings>
ConfigurationManager.AppSettings reads (as the name might suggest) the AppSettings block of your configuration.
Given you've created your own section, you want to use var section = ConfigurationManager.GetSection("Porject.Properties.Settings") and read the values from that section.
I'm using Mindspace.Lightspeed in my C# desktop application for the first time but I'm getting errors and unable to connect to database.
My App.config contains the following code:
<?xml version="1.0" encoding="utf-8" ?><configuration>
<configSections>
<section name="lightSpeedContext" type="Mindscape.LightSpeed.Configuration.LightSpeedConfigurationSection,Mindscape.LightSpeed" /></configSections><connectionStrings>
<add name="DefectsConStr" connectionString="Data Source=.\sqlexpress2k5;Initial Catalog=Defects;Persist Security Info=True;User ID=sa;Password=123"
providerName="System.Data.SqlClient" /></connectionStrings> <lightSpeedContext>
<add name="Development" connectionStringName="DefectsConStr" dataProvider="SqlServer2005"/> </lightSpeedContext> </configuration>
I have managed to reproduce your problem, you need to change your configuration file so that the section name is
lightSpeedContexts and not lightSpeedContext
see my configuration file
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="lightSpeedContexts" type="Mindscape.LightSpeed.Configuration.LightSpeedConfigurationSection,Mindscape.LightSpeed"/>
</configSections>
<connectionStrings>
<add name="Blog" connectionString="Data Source=Blog.dat"/>
</connectionStrings>
<lightSpeedContexts>
<add name="Blog" connectionStringName="Blog" dataProvider="SQLite3" identityMethod="KeyTable" pluralizeTableNames="false"/>
</lightSpeedContexts>
</configuration>