Cant find my EF code first database - c#

I am doing Microsoft's MVC getting started tutorial:
Getting Started with Entity Framework 6 Code First using MVC 5.
This includes the creation of a code first database.
It all works fine, but I am not able to find my database.
This is my ConnectionString:
<add name="MovieDBContext" connectionString="Data Source=.\SQLEXPRESS; Integrated Security=True" providerName="System.Data.SqlClient"/>
If I debug my index Method the connection is as follow:
Data Source=.\SQLEXPRESS; Integrated Security=True
But there is no Database in my SQLEXPRESS Instance, I have checked it with SQL Server Management Studio.
I also cant find anything if I search my filesystem for *.mdf.
App_Data in my Project is Empty...
But all CRUD operations are working fine, there has to be something.
The only way I can see that table is to connect to .\SQLEXPRESS via the Visual Studio Server Explorer. But where is this physically located? Why cant I see the table if I connect to .\SQLEXPRESS via SQL Server Management Studio?
Any idea?

You didn't specify the Initial Catalog in your connection string so probably you are using the Master database.
You need to specify the Initial catalog like this:
<add name="MovieDBContext"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=yourDBName;Integrated Security=True"
providerName="System.Data.SqlClient"/>

Related

Switch from LocalDB to SQL Server DB MVC5

I have a MVC5 project with individual authentication.
Now I want to move from (localdb)\MSSQLLocalDB to another SQL Server database.
I've tried to change connection string from this :
Data Source=(localdb)\MSSQLLocalDB;Integrated Security=True;Connect
Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False
to this:
Data Source=PC-GABRI;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False
And the application is still using localDB instead of my new SQL Server DB.
I have already migrated the database and roles.
I have also tried to comment all of my connection string form Web.Config but my application is still working and I can login with my "test" user.
I can't find anything on the web about this!
PS.
Related Questions: Switch LocalDB to SQL server db with MVC5
We have the same problem!
UPDATE 1
DB Instance:
Connection String:
The instances are running correctly.
Tell me if you need more info.
UPDATE
This Guide was very useful for me :
https://danieleagle.com/2014/05/setting-up-asp-net-identity-framework-2-0-with-database-first-vs2013-update-2-spa-template/
Follow all the easy steps for move your LocalDB table to SQL server db Table.
1. Inspect the Web.config File Connection String and Connect to Local Database
Change the defaut connection string:
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-WebApp-20190226025646.mdf;Initial Catalog=aspnet-WebApp-20190226025646;Integrated Security=True" providerName="System.Data.SqlClient" />
To your connection string:
<add name="DefaultConnection" connectionString="Data Source=PC-YWGSB;Initial Catalog=WebApp;user id=admin;password=admin" providerName="System.Data.SqlClient" />
2. Then use the commands for DB Migration "Codefirst"
This is the official guide:
https://learn.microsoft.com/it-it/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/migrations-and-deployment-with-the-entity-framework-in-an-asp-net-mvc-application
From the Tools menu, select NuGet Package Manager > Package Manager Console.
At the PM> prompt enter the following commands:
enable-migrations
add-migration InitialCreate
update-database
The database will be created automatically during migration.
If you already have one make sure to backup all tables.

Connection string not found with ASP.net

I'm trying to get my local SQL database connecting to my ASP.Net project and nothing I try is working.
The database is named cinemaDatabase and its using windows authentication.
In web.config I have
<connectionStrings>
<add name="myDatabase" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=cinemaDatabase;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
Then in a cshtml page I have
#using WebMatrix.Data;
#{
var db = Database.Open("myDatabase");
}
I keep getting this error
InvalidOperationException: Connection string "myDatabase" was not
found. OpenNamedConnection
What am I doing wrong here? Thanks in advance.
Edit -
Got it working, had to make a normal SQL server instead of the local MSSQLLocalDB one and change the data source in web.config to that.
Then used this code on my page and it connects now.
#using WebMatrix.Data;
#using System.Data.SqlClient;
#{
var connectionString = "Data Source=DESKTOP-58NGNQP;Initial Catalog=webDev;Integrated Security=True";
var providerName = "System.Data.SqlClient";
var db = Database.OpenConnectionString(connectionString, providerName);
}
I believe the problem is with your connection string. Unfortunately, I don't have a running example right this moment with an attached .mdf file, but I believe something like this will do the job for you, I grabbed this string from MSDN's article on connection strings
<add name="ConnectionStringName"
providerName="System.Data.SqlClient"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFileName=|DataDirectory|\DatabaseFileName.mdf;InitialCatalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True" />
Couple of important facts here:
Data Source=(LocalDB)\v11.0 - This specifies the start-on-demand localdb instance to use. Note that the version, v11.0 in this string, varies from installation to installation, and you should find the correct version for your string. (You should be able to find it easier below)
AttachDbFilename=|DataDirectory|\DatabaseFilename.mdf - this tells LocalDb to load that database file. Alternatively, localdb persists database files in your user home directory, or AppData directory somewhere.
Integrated Security=true - A connection string needs an authentication scheme. Two possible options are Integrated Security, which uses your windows credentials to attempt access to your database, which works well with many things, LocalDb being one of them. Another option is to use sql authentication and a sql name and password to connect.

How to divert a default SQL Server database for web application in C#?

I've created a new web app project template in VS community 2015, when I debug the project, it create its own default SQL Server database in MSSQLLocalDB with a default database name like aspnet-WebDBTest-20151026010545.
I've tried to change the debug database to a SQL Server Express database.
have changed web.debug.config file with new connection string on SQLEXPRESS
<connectionStrings>
<add name="ApplicationServices"
connectionString="Data Source=.\sqlexpress;Initial Catalog=TEST;Integrated Security=True"
providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
</connectionStrings>
I went to project properties and changed configuration (Active-debug -Any Cpu) changed Database Entry detail, set destination SQL Server database and destination connection string and verified.
I even went to Tools > Options > Database Tools > Data Connections
and set SQL Server Express instance name
But still when I run project in debug mode and add some user data, project is running its default database on MSSQLLocalDB
Any ideas? Am I running correct setting? (Please note I've created my destination database on SQLEXPRESS)

Problems with connecting to local SQL Server Network Interfaces, error: 26

I'm currently trying to connect from Visual Studio 2013 C# application to a SQL Server 2012 instance but I'm running into troubles. Namely error 26 that no connection could be created.
My question there is what could still be wrong with what I'm doing (I'll post the code snippets I use below and then also what I controlled so far / steps I took):
Code
I created an application config which has the following line:
<connectionStrings>
<add name="DefaultConnectionString"
connectionString="Data Source=TH-HP\SQLEXPRESS;Initial Catalog=BB;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False"
providerName="System.Data.SqlClient"/>
</connectionStrings>
I use an intermediary class I call Database to create the connection strings and run the commands.
When initializing it I use the following command (which sets the private variable connectionStr):
Database db = new Database(Properties.Settings.Default.ConnectionString);
Then when I try to open the connection:
SqlConnection con = new SqlConnection(this.connectionStr);
con.Open();
The error happens.
Steps
(as note both the program and the SQL Server instance are running on the same machine)
Correct servername
Correct catalogue name
SQL Server accepts remote connections
Server Browser is running
Server itself is running (connected to it via management console)
Like I said I'm running out of ideas what to check next.
this link may help: How to connect to local instance of SQL Server 2008 Express
also make a try with changing ConnectionString to:
<add name="DefaultConnectionString" connectionString="Data Source=TH-HP;Initial Catalog=BB;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False" providerName="System.Data.SqlClient"/>
or
<add name="DefaultConnectionString" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=BB;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False" providerName="System.Data.SqlClient"/>

Migrating the database file of ASP.NET MVC3

I just started using C# and MVC3, and I've been following the MVCMusicStore tutorial at ASP.NET website.. In the tutorial it used
<add name="MusicStoreEntities"
connectionString="Data Source=|DataDirectory|MvcMusicStore.sdf"
providerName="System.Data.SqlServerCe.4.0"/>
as the connection string... In my application I changed it to
<add name="FashionStyle.StoreEntities"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|FashionStyle.StoreEntities.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient"/>
But I found out the actual database file is in
C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA
My question is how do I move it from there to the App_Data folder in the project and make it so that Visual Studio 2010 will know where to look for the database file? This is because the application is a group work and I want to share it with my team members.
actually what they have used is .sdf file ie., Sql Server Compact Database file and what you are using is .mdf file which is a Sql Server Database file.
now if you have created that database either through VS Server Explorer or Sql Server Management Studio then I think you dont have to worry, just use
<add name="FashionStyle.StoreEntities"
connectionString="Data Source=.\SQLEXPRESS;
Initial Catalog=yourDatabase;
Integrated Security=True;
User Instance=True";
providerName="System.Data.SqlClient"/>
Visual Studio will only know where to find it if you tell it where to find it. Simply backup the database using standard SQL Server procedures and restore it where is most appropriate then update your appconfig/webconfig + Server Explorer to point to it.

Categories

Resources