I was following this to create a simple MVC application. After creating the model, when I tried to add controller, I got the following error :
Unable to retrieve metadata for "MvcApplication.Models.Movie". Invalid value for key "attachdbfilename".
Can someone tell why I am getting this error.
Update : I saw this wherein solution is provided by changing the providerName. But in my case it is already System.Data.SqlClient. Following are my connectionStrings :
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-MvcApplication-20130708120940;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
<add name="MovieDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
The error message is saying invalid value for AttachDbFilename which is part of your MovieDbContext connection string.
The connection string currently shows this AttachDbFilename=|DataDirectory|\Movies.mdf so it appears the mdf file is now missing from the App_Data folder.
I found the solution. the problem is the "|DataDirectory|". I Changed "|" to "\"
I just solved this problem. Even though I am new to MVC, I am bit embarrassed to say, I was trying a work around for another problem I was having and moved my SetInitializer into my classname : DbContext class.
The app worked just fine and the database was created fine as I changed the models but I couldn't add new controllers or modify current controllers using the scaffolding tool.
I began receiving errors. One of them is listed above and another is:
Unable to retrieve meta data for 'namespace.Models.class'. A file
activation error occurred. The physical file name '\name.mdf'
may be incorrect. Diagnose and correct additional errors, and retry the
operation.
CREATE DATABASE failed. Some file names listed could not be created.
Check related errors.
Moving the SetInitializer back into my Global.asax fixed the problem.
Related
I've done a lot of research on this, and tried a number of approaches, but I'm beginning to think that what I want to do can't be done. To wit -- I am building an application that will be used by multiple clients. Each client will have their own database connection requirements, and their own set of customizations to control some of the behaviour of the application.
What I want to do is to create a .config file for each individual client, then use a command-line argument that will specify which .config file to load. Each config file will have its own section, and its own . I've gone through much of the documentation con ConfigurationManager and the Configuration classes and tried a number of different approaches, all of which have failed.
The basic question is "when I open a .config file, how do I tell the application to make this .config the default configuration?" For example, when I later make reference to a value in appSettings, or a database connection string, how do make sure that these references will each map to the custom configuration I have loaded, and not the one created by default by Visual Studio?
I have found all kinds of references to loading configuration files, but nothing that tells me how to dynamically replace the standard one with the one I just loaded. I keep ending up with null reference exceptions and running headlong into brick walls.
You can have multiple connection strings defined in your config file, then refer to them by name.
Example:
<configuration>
<connectionStrings>
<add name="client1Connection"
connectionString="Data Source=(local);Initial Catalog=dbclient1;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="client2connection"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=dbclient2;Integrated Security=True"
providerName="System.Data.SqlClient" />
...
</connectionStrings>
</configuration>
You can refer to them in you c# code by:
string client1ConnStr = System.Configuration.ConfigurationManager.ConnectionStrings["client1Connection"].ConnectionString;
string client2ConnStr = System.Configuration.ConfigurationManager.ConnectionStrings["client2connection"].ConnectionString;
I configured my SQL Server database using aspnet_regsql.exe. Everything worked great. I then added the new connection string in my web.config file:
<connectionStrings>
<clear />
<remove name="LocalSqlServer" />
<add name="SQLConnectionString"
connectionString="Provider=ProvName;Server=Sname;Database=dbName;Uid=user; Pwd=pWord" />
</connectionStrings>
When I try to access the database from the website I get an error:
Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.
Parser Error Message: The connection name 'LocalSqlServer' was not found in the applications configuration or the connection string is empty.
Source Error:
I am running this site on IIS via Visual Studio 10. Any ideas what steps I'm missing?
<add name="LocalSqlServer" connectionString="Data Source=.;Initial Catalog=DBName;Uid=user;Pwd=pWord" provider="System.Data.SqlClient"/>
Something like above should work.
Your code is looking for a connection string named "LocalSqlServer", yet you are adding a connection string named "SQLConnectionString".
Change this to:
<add name="LocalSqlServer" connectionString="Provider=ProvName;Server=Sname;Database=dbName;Uid=user; Pwd=pWord" />
Do a search for the phrase "LocalSqlServer" in all your config and code (.cs?) files. It is being references somewhere else...not just the connection-strings config area.
The "name" attribute is used in other places in the code to look up the actual connection string.
I like Agent Ransack for looking in files for a specific string.
When you find the other reference, you can replace it with "MySuperSpecialConnectionStringName" (as seen below, an alteration of your original code).
I have a project in asp.net MVC with Entity Framework and my connectionsString at web.config
<add name="DefaultConnection" connectionString="data source=localhost;Initial Catalog=telexpo_bd;Integrated Security=SSPI;User Id=telexpo_bd;Password=hey76jdhdyU;" providerName="System.Data.SqlClient" />
<add name="telexpoEntities" connectionString="metadata=res://*/telexpoEF.csdl|res://*/telexpoEF.ssdl|res://*/telexpoEF.msl;provider=System.Data.SqlClient;provider connection string="data source=localhost;initial catalog=telexpo_bd;integrated security=True;user id=telexpo_bd;password=hey76jdhdyU;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
this work perfectly in my PC when I emulate via Visual Studio but when I publish my application in web server doesn't work. Give the error page built by me but I was connected my web server company and after long talks they give to me this error:
Configuration Error
Description:An error occurred during the processing of a configuration file required to service this request. Please review the
specific error details below and modify your configuration file
appropriately.
Parser Error Message: The connection name 'LocalSqlServer' was not found in the applications configuration or the connection string is
empty.
Source Error:
Line 255:
Line 256:
So, my web.config just have 193 lines and the error in web.config is at line 259!??
I was investigate by 'LocalSqlServer', "AspNetSqlMembershipProvider" and doesn't found nothing...
After some investigation across the web I found that one which generated the AspNetSqlMembershipProvider and LocalSqlServer was the machine.config at C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config so I was thinking at server was the same but I can't change it because I have a dedicated server.
After time and tried a lot of things...but I can't find the solution about it... they insist the problem is about connectionstring. How could be?
Can someone give some suggestions please?
Yes it is because of connection string, that it cannot find the connection string value under the name 'LocalSqlServer'. Try changing the connection string that matches the server in the app.config file. If you have separate projects for web and entity then include the connection string in the web.config file of web application which will resolve.
I'm trying to use a local db for my application, and want it to reside in a folder inside my application. Unlees I'm missing something, this shouldn't be a problem with user rights, since it is in the application folder.
The connection string is this:
<connectionStrings>
<add name="Calendario2DB"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=\AppData\Database1.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
I'm Using a code first aproach, so the database should be generated (filled) at run time, but I have a problem with
AttachDbFilename=\AppData\Database1.mdf
This should be pointing to a folder in my app (called appdata) but is not working with an error:
A file activation error occurred. The physical file name '\AppData\Database1.mdf' may be incorrect. Diagnose and correct additional errors, and retry the operation.
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
So how do I have to write the path name to the phisical file?
You should use the built in |DataDirectory| feature.
<connectionStrings>
<add name="Calendario2DB"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Then the database would be located in:
\Program Files\Application Location\App_Data\Database1.mdf
According to the documentation you cannot use relative paths without using |DataDirectory|:
The path may be absolute or relative by using the DataDirectory substitution string. If DataDirectory is used, the database file must exist within a subdirectory of the directory pointed to by the substitution string.
I've import my SQL 2005 database to my project and i am trying to connect to the database using the web.config
But I've this error when I add the connectionString to the web.config.
<add name="strConn" connectionString="Data Source=.\sqlexpress;Initial Catalog=intranet_db;Integrated Security=True"
providerName="System.Data.SqlClient"/>
HTTP Error 500.19 - Internal Server Error
The requested page cannot be accessed because the related configuration data for the page is invalid.
Detailed Error Information:
Module IIS Web Core
Notification Unknown
Handler Not yet determined
Error Code 0x80070032
Config Error The configuration section 'add' cannot be read
because it is missing a section declaration
Config File \?\C:\Users\user\Documents\Visual Studio
2012\Projects\test\test\web.config
Requested URL localhost:64198/
Physical Path
Logon Method Not yet determined
Logon User Not yet determined
Request Tracing Directory
C:\Users\user\Documents\IISExpress\TraceLogFiles\
Am I doing it wrong? What should I add in the web.config?
Thanks.
Please make sure you put the "add" tag in the correct place. I'm able to reproduce your error when I place it i wrong section. Your web.config file should look like this:
<configuration>
<connectionStrings>
<add name="strConn" connectionString="Data Source=.\sqlexpress;Initial Catalog=intranet_db;Integrated Security=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
it should be something like this
<connectionStrings>
<add name="connectionString" connectionString="Provider=SQLOLEDB.1;Data Source=dbhostname;Persist Security Info=True;Password='password';User ID='username';Initial Catalog=dbname" />
</connectionStrings>
have a look at http://www.connectionstrings.com/ it has examples of what they should be.
looking at you connection string make sure the instance name ./SqlExpress is correct.
A quick test would be to load sql manager and connect with the ./SqlExpress
Standard Security
Server=myServerAddress;Database=myDataBase;User Id=myUsername; Password=myPassword;
Trusted Connection
Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;
to give you a better answer would require knowing how you want to connect to the database.