I have a one entity framework object and when I add it to my project, the connectionstring
is added to app.config
in the connectionstring
section, but when I want to create new entitycontext
and use this connectionstring
, this error appears
I suspect that your issue is coming from the fact that you have more than one project in your solution and the one that contains your entity framework stuff including edmx
files is NOT the solutions's startup project. In this case even if the connection string exists in the EF app.config
project, still CLR cannot find it at runtime. For example, if you have a web site and a EF project in your solution, you need to copy the connection string from the EF project's app.config
to your website's web.config
. Basically, any connection string data should exist in the config file of the project that the .Net threads initiated from by CLR (i.e. your startup project).
If this is not your case, then just open your edmx
file, right click on its surface, select properties and copy the connection string and paste it into your app.config
Connection String section. This way you can make sure that you are having the correct one in your config.
EDIT:
As you can see here on
Documenation on ObjectContext Constructor, the first parameter is the connectionstring name which is code generated at the time you create your EDM. If, somehow, the name of your connectionstring name happens to be changed, all you need to do is right clicking on your model and selecting "Update Model From Database..." then follow the wizard to update your confing and designer to reflect this change.
You need to copy the connection string in the app.config to your web.config, or copy the entire file to the project which displays the output. It is one of the conditions to consume the framework.
I ran into this problem when I tried to put my custom database logic in a .dll to be used by multiple projects in my solution.
While the .dll had the correct app.config file, it didn't work. Entity frameworks wanted the connection information in the app.config of the .exe. Copying the information to there worked just fine.
Morteza's solution of pasting the connection string directly into the .edmx didn't work for me, as it wouldn't let me paste the value in there -- although that's precisely what I wanted to be able to do.
Hi I had this problem and it was making me nuts. Anyway finally I figured out what the problem was. First thing you have to do is make sure that the connectionstrings
in app.config
and web.config
are the same. Then you must double click on the .edmx
file so you can see the tables. Once u are click anywhere near the tables but not on the tables and go to properties. From the dropdown list select the ConceptualEntityModel
and search for the Entity Container Name and remember it well.
Next go to the designer of the edmx file and open the constructors. (the designer is the subfolder of the edmx file) the constructors should have two parameters in the BASE parameter
public DBEntities() : base("name=DBEntities", "DBEntities")
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}
this is one of them. the first parameter should have the name of the project file in which the .edmx
file is in. The second parameter must have the name of the entity container name from the properties I mentioned earlier about.
do not forget to arrange all the constructors with the : base("", "")
Atleast that was my problem and my problem was solved like that. I hope u manage to solve yours like this.
I had a variation on this that no-one seemed to cover.
I had a main project with a couple of models, and a Test Project containing unit tests. The Test Project was working, but then stopped with the error mentioned in the OP. I hadn't done any renaming or moving of the EDMX file.
A lot of the advice mentioned comparing .config files, but my project had none at all.
In the end, I copied the app.config file from the main project into my test project and then it worked. Whether this is the correct step, or will present maintainability issues when additional models are added, I do not know, but at least my unit tests are running correctly again now.
Although Morteza Manavi' answer does solve this problem, another solution is to build the connection string dynamically and pass it into the constructor for your ObjectContext:
public static string CreateConnectionString()
{
var assemblyPath = Assembly.GetExecutingAssembly().Location;
string assemblyLocation = Path.GetDirectoryName(assemblyPath);
string dbPath = Path.Combine(assemblyLocation, "YourDatabase.sdf");
var sqlBuilder = new SqlConnectionStringBuilder { DataSource = dbPath };
var entityBuilder = new EntityConnectionStringBuilder
{
ProviderConnectionString = sqlBuilder.ConnectionString,
Provider = "System.Data.SqlServerCe.3.5",
Metadata = @"res://*/YourModel.csdl|
res://*/YourModel.ssdl|
res://*/YourModel.msl"
};
return entityBuilder.ToString();
}
// Snip...
var entityContext = new YourObjectContext(CreateConnectionString());
This eliminates the need to copy the connection string information to the app.config of your startup project which, at least in my case, was not desirable.
I forgot to add providerName="System.Data.EntityClient" as attribute in the connection string. This resulted in this error so
<add name="connectionName" connectionString="metadata=res://*/..." providerName="System.Data.EntityClient" />
instead of
<add name="connectionName" connectionString="metadata=res://*/..." />
I just found that if an app is created in IIS from VS2010 two levels from the website root this error would occur. Not sure why it happens, would need to investigate more.
For example, if your app is in this path: /admin/advertiser
the error would appear if you don't have /admin
virtual directory in your IIS site.
All I did is created an empty admin
directory in my .../intepub/wwwroot
error disappeared.
You will find that you won't be able to start debugging until you do the step above.
We had this problem in our team in past, it took some time to remember but this was exactly how we fixed it before also.
I had a class library that didn't want to work with EF either. After I copied the app.config (or just the connectionstring section) from my class library to the exe project the connection worked fine! Probably the config file is expected to be in the same folder as the exe project and therefore was not found. So always be extra careful when a config file is used in a class library project!
I am using n'tier architecture and got the same problem but this one help me.hope this will help u.
First you have same connection string
on you libraries
where you can access DB like in app.config
and web.config
after that you simply add a overloaded constructor in .edmx(Model.context.cs) file that now you have two constructor one is default and the other one u just added (overloaded).
public YourEntityName(string connString)
: base(connString)
{
}
Well... this problem could be also for a very simple (dumb) reason... I copied a file from another project and forgot to change the ConnectionString on the EntityDataSource... as I was at the beginning of the project and happened in the login page, I thought it was something on the config, but was just the wrong connection string name (and DefaultContainerName).
So I had a similar issue with JetBrains Rider. What I was doing was using the green ▶ button next to the main
function. This was causing the above message to show. If I instead right clicked on the project itself and clicked run, then it worked.
Not sure why this worked by I'm assuming it's because Rider does not read the app.config if running the individual function?
© 2022 - 2024 — McMap. All rights reserved.