Configuring local db location in Entity Framework - c#

I am trying to configure a different location for my local db.
I read an article on configuring connection strings and it suggests I can replace |DataDirectory| in the web config with ~/MyDirectory where ~ is the web app root. So I have input the connection string as:
<connectionStrings>
<add name="Context" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=Context-20130829161219;Integrated Security=SSPI;AttachDBFilename=~/MyDirectory/Context-20130829161219.mdf"
providerName="System.Data.SqlClient" />
Unfortunately, when I run the app it doesn't seem to create the db at all. I can imagine it's only something very small that I am doing wrong but I would appreciate if someone can point me in the right direction.
Many Thanks

Lets try with below string it will help you
<add name="Context" connectionString="metadata=res://*/Models.Datacontext.csdl|res://*/Models.Datacontext.ssdl|res://*/Models.Datacontext.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDb)\SQLInstance;initial catalog=Context-20130829161219;persist security info=True;user id=sa;password=Yourpassword;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
or replace your Source=(LocalDb)\SQLInstance;initial with Source=.\SQLInstance;initial

Try running the Update-Database cmdlet from the package manager console - you should at least see some output indicating why it doesn't work.

Related

Published website: an attempt to attach an auto-named database for file...failed

I'm trying to publish a website with a locally stored database on my localhost. The problem is that I can't enter into any page who have a database embedded as a data source. All databases are kept into the App_Data folder, and I've used both into attachdbfilename:
1. |DataDirectory|/Database1.mdf
2. C:/Users/username/Desktop/websitefolder/App_Data/Database1.mdf
I've tried the other topics to solve my problem, but they are not mentioning that's a published website. The site works without any problem on localhost with the port assigned, but any page with a database/source related shows this error.
There is my config file:
<add name="ConnectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf; User Instance=false;Integrated Security=false" providerName="System.Data.SqlClient" />
<add name="connect" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database2.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
Try adding the "InitialCatalog=DatabaseName;" to your connection string:
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFileName=|DataDirectory|\DatabaseFileName.mdf;InitialCatalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True"
Here are some examples from the MSDN website to refer to.

attempt to open un named db failed. file with the same name exists, or it is located on UNC share

Actually i get a project from another pc and it has a database using local db having database with visual studio. before that i always used sql server for database. so i got stuck in it.
<add name="FZDatabase" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Talha\Desktop\Fashionzonefyp\Fashionzonefyp\App_DataFZDATABASE.MDF;Integrated Security=True" providerName="System.Data.SqlClient" />
here is the connection string. help as a totally new comer. any further suggestions and guidlines are also wellcomed... :)
Try this:
<add name="FZDatabase" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=C:\Users\Talha\Desktop\Fashionzonefyp\Fashionzonefyp\App_DataFZDATABASE.MDF;Integrated Security=True" providerName="System.Data.SqlClient" />
Actually there was a problem with my db name in connection string. and secondly my db was not in the directory of local db's which is by default (C:\Users\"Your pc name"\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\MSSQLLocalDB). thanks for help. :)

I have two web.config files. Which one should I use?

I recently built an application using visual studio express. This application was built using a local DB provided by VS express. I now have a subscription (free trial) to Microsoft Azure. What changes do I need to make to my project in order to point my project to the newly created Azure DB?
I also noticed I have 2 web.config files. one in the root directory and one in the views folder.
I believe I have to make an update to my connection string, but in a specific config file or both?
Here is my root directory web.config connection string:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source= LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-MvcMovie-20160922101748.mdf;Initial Catalog=aspnet-MvcMovie-20160922101748;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="MovieDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
and my (views) web.config connection string:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-MvcMovie-20130603030321.mdf;Initial Catalog=aspnet-MvcMovie-20130603030321;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="MovieDBContext" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True"
providerName="System.Data.SqlClient"/>
Any help would be highly appreciated.
I believe only one web.config with connection strings should be enough. Keep the one in the root.
Download Azure DB connection strings from the Azure portal and replace connectionString values in the web.config file with the Azure DB connection string(s).
You might also need to whitelist your machine's IP address in the Azure Portal to allow connections to Azure DB.
You only need to change the web.config on the root folder as thats the one used by the application.
And you are right, you need to point the connection string to your Azure database.
You definitely only need to update the one web.config in the root of the application with the connection string. The other web.config in the views folder is needed for your views to use html helpers and to make routing work as you expect in an mvc application so do not remove it.

host a database website with entity framework in the production server

I developed an asp.net website (using webforms) and for database connection I used Entity Framework. On my local computer everything is working fine. Now I try to host it I am having a problem. I have copied my files to the production server and attached my SQL Server database and set my connection string to:
<add name="ConnectionString"
connectionString="Data Source=SQL5003.Smarterasp.net;Initial Catalog=DB_9C4561_WFormBlog;User Id=DB_9C4561_WFormBlog_admin;Password=mydbpwd"
providerName="System.Data.EntityClient" />
but when I test my website am getting error:
Keyword not supported: 'data source'
When I removed "data source", it started complaining of "Initial catalog" keyword not supported.
Please help me out. You can refer me step by step tutorial or material that can put me through. Thanks in advance!
Try the below format, I have my database on smarterasp.net and it works well
<connectionStrings>
<clear />
<add name="ConnectionString" connectionString="Persist Security Info=False;database=DB_9C4561_WFormBlog;server=SQL5003.Smarterasp.net;user id=DB_9C4561_WFormBlog_admin;password=mydbpwd;Current Language=English;Connection Timeout=60;" />
</connectionStrings>

Connection to SQL Server Database inside Visual Studio connectionString error

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.

Categories