I am getting the following error trying to connect to DB2 Entity with .Net 4.0
The underlying provider failed on ConnectionString.
I get it on this line within the generated code of my entity cs file
public DOCUMAKRContainer() : base("name=DOCUMAKRContainer", "DOCUMAKRContainer")
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}
My App.config (which is created when you create entity the looks like this
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections />
<connectionStrings>
<add name="DOCUMAKRContainer"
connectionString="metadata=res://*/Documakr.csdl|res://*/Documakr.ssdl|res://*/Documakr.msl;provider=IBM.Data.DB2;provider connection string="Database=DCCPDEVL;User ID=documakr;Password=Dcpd#123;server.com:60000"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
I have tried to delete and rebuild the entity and get the same results every time. All of this is generated code when you add an entity object, so I am wondering if it is a VS 2010/IBM DB2 issue. Internet has not really given me any help.
Thoughts?
This site is a good resource: http://www.connectionstrings.com/
It looks you have have some garbage in your connection string:
provider connection string="Database
Actually, it looks like you've got a conection string jammed into your connection string.
Also, you might want to edit your post and remove or ##### the login info.
Related
I decided to learn how to make a simple ASP.Net project, with a reference to a database project through the Repository Pattern.
I have my Controller calling for a List<Weight> to handle:
public IActionResult MyWeight()
{
var repo = new Database.Repositories.WeightRepository();
var data = repo.GetWeight().Result;
return View(data);
}
When repo.GetWeight() is called, I get an AggregateException error, with an inner exception saying:
"No connection string named 'MyDatabaseConnection' could be found in the application config file."
So for clarity, let me outline the solution's structure:
aspProj
Controllers
Views
Service
App.config (1)
Web.config
...
Database
Entities
Repositories
App.config (2)
...
Database.Test
Test.cs
App.config (3)
...
I've added the following connectionString to all App.configs and the Web.config:
<connectionStrings>
<add
name="MyDatabaseConnection"
connectionString="Data Source=(localdb)\MSSQLLocalDB;Integrated Security=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
I have tested the database both from Visual Studio's Server Explorer, and through the Test.cs file from the test-project. I can insert data and retrieve without a problem.
But when the ASP.Net-part wants to access it, there is no love.
I thought it might be the ISS which did not know the path from where it is...
Any thoughts?
__
Edit:
My Web.config:
My AppSetting.json:
Well the problem is clear - ASP.NET is trying to access the database using a connection string with the name MyDatabaseConnection:
<connectionStrings>
<add name="MyDatabaseConnection" connectionString="put the connection to the db here..." />
</connectionStrings>
And in your Web.config you only have a connection string with the name WeightDatabaseConnection:
<connectionStrings>
<add name="WeightDatabaseConnection" connectionString="put the connection to the db here..." />
</connectionStrings>
Just add a new element for MyDatabaseConnection under <connectionStrings> in the Web.config file and it should work
Solution: Add the directory to your connection string in the app.config file and the Settings.setting file in the properties section of your project. My working connection string ended up being <Value Profile="(Default)">Data Source=(LocalDB)\v11.0;AttachDbFilename=F:\hi\prgrm\ProgramName\Database1.mdf;Integrated Security=True</Value>
Once I run my program I get the following error:
An attempt to attach an auto-named database for file F:\Graded unit
2\SimplyRugby\LollipopUI\bin\Debug\Database1.mdf failed. A
database with the same name exists, or specified file cannot be
opened, or it is located on UNC share.
The method that makes the error happen:
public bool CheckUsername(string username)
{
var usernameResult = (from person in dbContext.Persons
where (person.Username == username)
select person.Username).FirstOrDefault();
//stores username if a username is found
return !(string.IsNullOrEmpty(usernameResult));
// if no correct user found from query return false else true
}
After some research apparently it's that the connection string is wrong. I had a little play with some suggestions online but I'm not too sure what is incorrect and how to fix it so I've been going around in circles for the past five or so hours.
My app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="LollipopUI.Properties.Settings.Database1ConnectionString"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;"
providerName="System.Data.SqlClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
Thank you for your time.
Try to make User Instance property as true like
User Instance=True
in your connection string.
You can also refer to this related thread which says that your connection string should looks something like this:
<connectionStrings>
<add name="Sales_DBEntities"
connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='server=YourServerNameHere;database=Sales_DB;integrated security=True;App=EntityFramework'"
providerName="System.Data.EntityClient" />
</connectionStrings>
I have faced that situation and I resolved it like this.
Go to Setting of DataBase and set option **Copy to output Directory** to **Copy if Newer**
the problem will solve
Late response is better than no response at all:
The issue was that my connectionString wasn't mapped to the database location.
Edit: I'm not sure what the path I used was as it was so long ago but if you have this issue try using a full hardcoded path to the .MDF file. After you've confirmed it works then you can start fiddling around with it.
i registered my website on somee.com. for that i have uploaded MS SQL database. i was wrting this connection string in my code:
connectionString="metadata=res://*/nrcsaEntities.csdl|res://*/nrcsaEntities.ssdl|res://*/nrcsaEntities.msl;provider=System.Data.SqlClient;provider connection string="data source=My-PC;initial catalog=nrcsa;integrated security=True;multipleactiveresultsets=True;App=EntityFramework""
now as i registered somee.com is providing me new connection string that is:
workstation id=nrcsadb.mssql.somee.com;packet size=4096;user id=DuaZoya_SQLLogin_1;pwd=abcd;data source=nrcsadb.mssql.somee.com;persist security info=False;initial catalog=nrcsadb
i have changed connectiong string in file web.config by replacing this first connection string with provided connection string by somee.com
PROBLEM:
This replacement is generating warning that:
System.ArgumentException: Keyword not supported: 'user id'.
how to solve this problem?
In the web.config file ....
<connectionStrings><add name="nameofConnection" connectionString="Data Source=servername; Initial Catalog=DatabaseName; User ID=UserName; Password=Password;"/> </connectionStrings>
<system.web>
<compilation debug="false" targetFramework="4.0" /> </system.web>
you can edit target Framework according to you.
from : http://dotnet-developers-cafe.blogspot.in/2013/08/create-connection-string-in-aspnet.html
You're using Entity Framework.
Entity Framework has its own connection string which contains a reference to the EF metadata (metadata=...) as well as the inner connection string to connect to the actual database.
You need to insert your actual database connection string inside the EF connection seting, in the provider connection string=... section.
You will also need to add multipleactiveresultsets=True to their connection string; EF needs that setting.
As you are using entity famework, then your connection string will look like
<connectionStrings>
<add name="BlogContext"
connectionString="metadata=res://*/BloggingModel.csdl|
res://*/BloggingModel.ssdl| res://*/BloggingModel.msl;
provider=System.Data.SqlClient
provider connection string="data source=[you somee.com connetion string];"" providerName="System.Data.EntityClient" />
</connectionStrings>
what you need to do is simply change the value of data source from the actual connectionstring provided by somee.com
Don't replace the whole connection string. You will need to remove the Integrated Security = true section and replace it with user=DuaZoya_SQLLogin_1;password=abcd.
Also change the data source to nrcsadb.mssql.somee.com.
You pretty much just need to replace values in your existing connection string with the values provided.
I am trying the following solution and not having much luck:
How can i update app.config connectionstring Datasource value in C#?
The code I have is:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Because it's an EF connection string it's not a normal connection string
// so we pull it into the EntityConnectionStringBuilder instead
EntityConnectionStringBuilder efb = new EntityConnectionStringBuilder(
config.ConnectionStrings.ConnectionStrings["WindowsFormsApplication1.Properties.Settings.TestDBConnectionString"]
.ConnectionString);
// Then we extract the actual underlying provider connection string
SqlConnectionStringBuilder sqb = new SqlConnectionStringBuilder(efb.ProviderConnectionString);
// Now we can set the datasource
sqb.DataSource = "|DataDirectory|\\TestDBa.sdf";
// Pop it back into the EntityConnectionStringBuilder
efb.ProviderConnectionString = sqb.ConnectionString;
// And update...
config.ConnectionStrings.ConnectionStrings["WindowsFormsApplication1.Properties.Settings.TestDBConnectionString"]
.ConnectionString = efb.ConnectionString;
config.Save(ConfigurationSaveMode.Modified, true);
ConfigurationManager.RefreshSection("connectionStrings");
My app.config file has:
<?xml version="1.0" encoding="utf-8" ?><configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="WindowsFormsApplication1.Properties.Settings.TestDBConnectionString"
connectionString="Data Source=|DataDirectory|\TestDB.sdf"
providerName="Microsoft.SqlServerCe.Client.4.0" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup></configuration>
Where am I going wrong?
I've only been fiddling with SQL CE for a few days myself, but if you're doing anything with SqlCE database, you probably want to use SqlCe classes rather than Sql ones - try SqlCeConnectionStringBuilder?
Other than that, as long as |DataSource| is supported in CE the connection string you've posted looks like the examples I've seen & used.
I ended up creating my own XML file which stores the database string and lets the user update it if it is invalid or if they wish to connect to a different database. I think it works out better than trying to update the app.config.
Heya, I'm currently trying to get bltoolkit working in my project.
I've added the BLToolkit.3 project to my solution and am referencing it appropriately.
The code in question is really simple.
public List<Account> LoadAccounts()
{
using (DbManager db = new DbManager("MySql"))
{
var query = new SqlQuery<Account>();
return query.SelectAll(db);
}
}
With an app.config of
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="bltoolkit" type="BLToolkit.Configuration.BLToolkitSection, BLToolkit.3"/>
</configSections>
<bltoolkit>
<dataProviders>
<add type="BLToolkit.Data.DataProvider.MySqlDataProvider" />
</dataProviders>
</bltoolkit>
<connectionStrings>
<add name="MySql" connectionString="Server=localhost;Port=3306;Database=BLT;Uid=root;" providerName="MySql.Data.MySqlClient"/>
</connectionStrings>
</configuration>
When I try to run that, I get the following exception: "The type initializer for 'BLToolkit.Data.DbManager' threw an exception."
Upon closer inspection it seems to be line 261 in DbManager.Config.cs
Type dataProviderType = Type.GetType(provider.TypeName, true);
Which basically fails to get the MySQL providers type. I've tried putting the MySQL.data.dll in my applications run path, with no luck.
Any ideas?
By default, MySqlDataProvider is not included in build. You'll have to download source code and add MySqlDataProvider.cs as "Existing Item" to your project (and modify assembly-qualified type name appropriately). Or, if you're fine with recompiling BLTooklit, you can include it in BLToolkit project itself.