First I read this question, but this knowledge did not help to solve my problems.
In initial I've created edmx file by Visual Studio. Generated files with names:
uqsModel.Designer.cs
uqsModel.edmx
This files are located on App_Code folder.
And my web app work normally.
In Web Config generated connectionstring automatically.
<add name="uqsEntities" connectionString="metadata=res://*/App_Code.uqsModel.csdl|res://*/App_Code.uqsModel.ssdl|res://*/App_Code.uqsModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=aemloviji\sqlexpress;Initial Catalog=uqs;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" /></connectionStrings>
Then I had to generate classes by the instrument edmgen tool(full generation mode).
Generated new files with names:
uqsModel.cs
uqsModel.csdl
uqsModel.msl
uqsModel.ssdl
uqsViews.cs
it save new classed to the folder where edmx files located before, and remove existing edmx files. And when page redirrects to any web page server side code fails.
And problem: Unable to load the specified metadata resource.
Some idea, please.
The problem seems to be associated with the fact that you have generated metadata in the form of .csdl, .ssdl, and .msl files, and your connection string points to the resource that should be embedded to the project dll (which is likely to be unavailable in your Web Site).
The solution is to correct the path to metadata like the following:
metadata=~\App_Code\uqsModel.csdl|~\App_Code\uqsModel.ssdl|~\App_Code\uqsModel.msl
More information is available here.
Related
Just a quick question. I have a solution splitted in multiple projects. In one project, I have the database interactions and my EDMX. In this project, I have my app.config file with some connections strings.
This project, is imported as dependancy in a Web project. In this one, I have my Web.config where are defined (or "overriden") connections strings.
I'd like to know what are the mechanisms used to configure the database connection. From what I understood, the Web.config has all priority over App.config. But what I'm wondering is, is the App.config in dependancies projects used at compilation time ?
For instance :
Project A => app.config :
<connectionStrings>
<add name="A" connectionString="myConnectionStringA"/>
<add name="B" connectionString="myConnectionStringB"/>
</connectionStrings>
And the same in Web.config but with :
<connectionStrings>
<add name="A" connectionString="myConnectionStringC"/>
<add name="B" connectionString="myConnectionStringD"/>
</connectionStrings>
Which one will be used to define the connection to the EDMX ? In one hand, at compile time, logically it would be A & B used to define it, and C & D would be used at runtime.
But i'm not sure about it and for me, once the dll is "configured", I don't see how can C and D be used instead of A & B.
Could someone explain it to me please ?
Thanks !
The config file that is used at runtime is the one related to where you are launching your application. If you launch the projectA it will be the App.config file.
Actually it will be the file generated by the compilation on the proper directory "Debug" or "Release"
When you run your web project it will be the Web.config file there.
The dll isn't "configured" with the values from the config file, they are read when the application starts running and this will depend on the application that is running.
That is why if you change the values they will change when you relaunch the application without any need to recompile the project.
When designing your entities in Visual Studio, the connection string that is stored in the app.config file of the project is used.
Even though you add a reference to the project from the web project, the app.config of the referenced project is not used at all in the context of the web project. Of course, it can be used as a blueprint when adding the connection strings to the web.config.
The config file that is relevant to the web project is the web.config. So when running or publishing the web project, the settings that are used are the ones in the web.config.
They do not override the settings of the app.config in the sense of a fallback like "if the connection string is not configured in the web.config file, then I use the ones stored in app.config".
It is required that you add the connection strings that you want to use when running the web project to the web.config file, otherwise you'd encounter an error if you used the Entity classes.
For details on configuring ASP.NET web applications, see this link.
But i'm not sure about it and for me, once the dll is "configured", I
don't see how can C and D be used instead of A & B.
The config values are fetched when the progam is running, not when it is compiled.
Set & settings:
I use Entity Framework 5 and have a dll project with edmx file. In this project I have App.config with connection string for the EF model. I have also second project, ASP.MVC 4 web application which is a startup project. It references the database project. Important thing is - db is Oracle and EF uses Oracle custom providers.
Problem:
If I place my connection string in the ASP.MVC startup project is works fine. It's common advice to do this. But I don't want to. I don't see reason why I should. How can I force MVC/EF to find the connection string in App.config of the external library (which as a matter of fact is a data access layer)?
App.Config is used by WinForms, WPF and executable applications.
Web.Config is used in IIS (and is able to set IIS environment specific configurations)
It seems no App.Config will ever be merged to the Web.Config (source):
In using an App.config, file the configuration system merges the
App.config file with content of the Machine.config file when the
application starts and the configuration is applied. This mechanism
allows machine-wide settings to be defined in the Machine.config file.
The App.config file can be used to override the settings of the
Machine.config file; you can also lock in the settings in
Machine.config file so that they get used. In the Web.config case, the
configuration system merges the Web.config files in all directories
leading up to the application directory into the configuration that
gets applied. For more information about configuration and the setting
priorities, see topics in the System.Configuration namespace.
Perhaps you'll find a solution more appropriate to your needs by using Application Configuration Files.
Finally, after facing multiple issues, I've decided to move connection string of DAL into the Web.config of the web application. I was convinced by some arguments you can read here in the post of Chris Ammerman.
I tried this on two different computers but get the same issue.
I am using the latest and greatest visual studio 2013.
I create a new project (of type Web->Asp.Net application)
Choose a template type of MVC or Web API (have same issue whatever type of template I select).
Then I change the authentication type to use individual.
The project with all the authentication code is created.
Web.config has the connection string set for a .mdf file.
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WebApplication1-20131031102303.mdf;Initial Catalog=aspnet-WebApplication1-20131031102303;Integrated Security=True" providerName="System.Data.SqlClient" />
But the mdf file is missing in the app data folder. I searched the whole computer for the mdf file, but it is not created anywhere.
As a result testing my sign in and register modules using a browser gives me the Yellow Screen Of Death with the db connection being the issue.
What am I missing here?
It should be in
C:\Users\[*yourusername*]\Documents\Visual Studio 2013\Projects\WebApplication2\App_Data
This is not a real answer to my question as to why this is happening. But here is the workaround I did to make it happen. I downloaded one of their sample projects which did have the mdf file in the app_data folder. Then I got the table schema from that mdf file and created those tables in my SQL Server database. Then I changed my connection string to point to my SQL Server database. My authentication pages worked after that with this new database. But what a pain to go through.
I am having a linq to sql problem with a C# 2010 windows form application when it is changed to a dll and accessed by other applications. The purpose of the calling the code as a dll is for performance reasons and to share the code accross multiple applications.
When the application is on its own, it runs fine.
Basically when the linq to sql was dragged to the desktop, a default linq to sql connection was setup by the .net framework. However I changed the *.designer.cs to use the system.configuration namespace,
added a reference to the system.configuration namespace, and changed the default linq to sql connection to use the database connection obttained in app.config file. I also removed any refernces to the default database connections in the the 'properties or 'settings' section for each project file folder.
Now the problem is the default setting values to the database are reset by the .net framework. Thwe values in the app.config file are no longer being used.
Can you show me code, point me to a reference, and/or tell how I can make this application is the connection string values in the app.config file again?
A dll is run in the the context of the application calling it. It is not an application in its own right, so giving it an app.config file is useless. The configuration settings used by the dll are supplied by the calling application's .config file (it can be an app.config file or a web.config file). In your case, any application using the dll must have the database connection defined its own .config file.
Here's a screenshot of the error:
The error itself is:
The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.
Going to the app.config file of that same project I can find:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="DocumentsEntities" connectionString="metadata=res://*/Documents.csdl|res://*/Documents.ssdl|res://*/Documents.msl;provider=System.Data.SQLite;provider connection string='data source="D:\Programming Projects\Desktop Applications\C#\DocumentScanner\DAL\Documents.sqlite"'" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
The connection string does exist but I'm getting this error.
I'm using SQLite and .NET Framework 3.5.
Any suggestions?
Have you seen this answer? Does it help?
http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/f5904b4d-b2f8-421e-90de-339f93959533/
Quoting:
My only guess is the app.config is not
in the binary directory where the exe
is. Please do the following:
1. Visually verify that the app.config with the expected content
is in the directory where the exe is
compiled. (Existence in the project
root directory is not enough.)
2. Use System.Configuration.ConfigurationManager
from within your app to examine the
content of the app.config your exe is
using. I’m also looking at the
content of the connection string, and
I can say that it may not work in a
multi project environment (unless
you’ve duplicated the EDM in each
project). The reason for that is “.”
resolves to the directory where the
exe is loaded from. If you want to
reuse the same EDM, you at least have
to make a few steps back in the path
and then navigate to the project where
the EDM is, e.g.
“......\Proj1\AdventureWorksModel”.
Additionally you may consider using
the |DataDirectory| macro - when you
load an AppDomain you can set
|DataDirectory| to point to the exact
directory where the EDM is, and then
use that in the connection string,
e.g.
“|DataDirectory|\AdventureWorksModel”.
If you are working on an ASP.NET
project, you can use “~” which refers
to the project root. In that latter
case, you can’t reference a model
outside your project’s hierarchy
though.
Zlatko Michailov Program Manager,
Data Programmability Runtime Microsoft
Corp.