I am following along this tutorial! everything works great, the blog names saved and retrieved from the database (assuming) since they are displayed in the console. Yet there I don't find any database created for it in local db nor the sql express on my machine. How to find where is the database created in the code first approach.
You can get the connection string by using ctx.Database.Connection.ConnectionString which will tell you what database you are using.
I was having a very similar problem here. My connection string was set to .\SQLEXPRESS and a generated Initial Catalog name, but when I connected to .\SQLEXPRESS using SSMS, nothing showed up.
Using IntelliTrace in VS 2010, I was able to see the ADO.NET command, and in the Autos grid, it showed me the SQL command and the following connection string: Data Source=(localdb)\v11.0;AttachDbFilename=|DataDirectory|(my_context_name_here).mdf. Then, using the Server Explorer I can connect to (localdb) and see the data.
This problem happened because my context was derived from DbContext, but it did not used the base(connectionStringName) constructor (like the UsersContext that comes with the MVC4 template does). Changing that, I can see the database in .\SQLEXPRESS.
Related
To start off, I'm new to Visual Studio and DB2, so please bear with me. I've been reading different books and will be starting classes soon, but I'm completely stumped with this.
I have a webform set up using VB.
In the web.config file, I've added <add name="rfqAS400TEST" connectionString="Server=serverIP; Database=RFQTST; UID=userid; PWD=password; CurrentSchema=RFQTST;" providerName="IBM.Data.DB2"/> inside of the <connectionStrings> tag. However, when I look at the connection in the Server Explorer, the tables dropdown shows HUNDREDS of tables, none of which are the table I referenced in my connection string.
I've tried to modify the connection and filter by the schema and DbName with no luck - it still only shows the hundreds of other tables instead of the one I want.
When I manually specify a custom SQL statement (SELECT * FROM <library>.<filename/table>) in the GridView Configure Data Source wizard, the GridView displays the records as it should, so I know the connection to the correct library and file is working.
It just seems strange that I have to manually type the statements because the correct table fails to show up in any of the wizards or the Server Explorer. Any suggestions or thoughts would be GREATLY appreciated.
Update
Out of curiosity, I tried to set up the connection using the ODBC datasource and the system data source name as IBM i Access for Windows ODBC. This displays extra tables as well, but it includes the tables that I've been wanting all of this time. This just confuses me more. Does that mean the DB2 connection (even though it technically is connected) is set up incorrectly?
The CurrentSchema is just a proxy for the SET CURRENT SCHEMA, to name the implicit schema qualification, for unqualified table-references. The tables that appear in an ODBC API request such as via SQLTables(), are unrelated to that current_schema.
A generic [db2] provider does not acknowledge\reflect the distinct attributes of the DB2 for i, whereby the database is [in a simplified sense] the entire system; the catalog API calls reflect either that, or that the schemas that are part of the library list concept. With the IBM® i Access ODBC driver, there are Connection string keywords for Connection Properties and Server Properties.
The following Redbooks publication and SQLTables Description may have some value.
I created website using asp.net MVC with Entity Framework Code First. It worked before on Windows Azure, the site was available publicly.
For some reason, I deleted database that was used to store data of my webpage. I created new one, similar to previous one.
I properly connected my site to database (in solution I clicked "Publish", in "Settings" there is place to type data about database, in "Destination Connection String" window I typed all needed data and clicked "Test Connection" - everything seems to be fine)
I published my site, the site works correctly, but when I go to page that gets data from database is see this error:
Invalid object name 'dbo.AspNetUsers'.
In Visual Studio in "SQL Server Explorer" I can see what is in my database. In fact, there is nothing inside (there is one table: "__MigrationHistory" and as I suspect this is created by default).
How can I generate all those tables again? It should generate database structure automatically.
PS: Accidentally I deleted "migations" from my project, do you think this is the cause?
This is not my projects blame, it works OK on localhost on my computer, when I delete localDB, it generates new one without any problem.
Yes, you need migrations. But you can always re-create them (well, if you removed them all it will be initial migration containing all of your previous migrations).
To quick fix you can backup and restore you database on the server. And if you need to re-create migrations do the following:
Backup all your data
Delete the entire database (local and remote), delete all migrations from code.
Create databases (local and remote one).
Run "add-migration Initial" command from PM console
Run "update-database" from PM console.
Upload your code to the server again. If database is empty, new initial migration should be automatically applied.
I'm using Code First concept in Entity Framework and I'm constantly getting the following exception while starting application:
Cannot attach the file 'C:\Users\Admin\Documents\Visual Studio 2012\
Projects\Pro\Pro.Web\App_Data\Pro.mdf' as database 'Pro'.
I've put this in Global.asax.cs but also without success:
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<ProWebContext>());
var ctx = new ProWebContext();
ctx.Database.Initialize(true);
I've checked under App_Data directory and there no database created. Also, under Server Explorer there is nothing under Data Connections. Everything was working fine yesterday and today not working at all. I've tried to connect to LocalDB with SQL Server Management studio but it says it cannot connect to local database. Any ideas what could be a problem?
Thanks!
I found the solution. With newest SQL Server Management studio there is no problem in connecting to the local database. Connection needs to be established like this:
After logging in we can still see old database present even if there is nothing under App_Data directory and under Data Connections in Server Explorer in Visual Studio. When we delete that database from SQL Server Management studio and start application again there will be no more errors while attaching database.
I was facing the same error when I saw this but I couldn't delete the database using SQL Server Management Studio so I remembered IDatabaseInitializer.
Setting a database initializer for my Code First context solved the issue. In my case adding an initializer to always drop the DB worked (I added the code in the static constructor of my context, most people add it in Global.asax):
static SomeDbContext()
{
System.Data.Entity.Database.SetInitializer(new DropCreateDatabaseAlways<SomeDbContext>());
}
There are other initializers of course.
I just got into the same problem, and yet the answers above helped, the solution was different.
In my case, I was reusing a project that has already created it´s database. And since the name in the config was the same, it was throwing the exception, missleading the real solution.
It worth checking the database is not created and is different than the ones you used before. Change the name and go ahead.
I deleted the mdf file manually so the code will recreate it again, and had the same problem. I solved it by running update-database command in the package manager console.
I am on VS 2012 RTM, with EF 5. I am doing Code First, but trying to ignore Code Migrations since I am just in development. To avoid them, I have this set
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<SomeContext>());
Occasionally, even when I don't think I've touched the model, it decides to recreate. That's fine. But it usually results in the error
Cannot drop database because it is currently in use.
So I decided to delete the database entirely. I go into VS, go to the "SQL Server Object Explorer", find the DB and delete it. Now I'm stuck at
Cannot attach the file '{0}' as database '{1}'
I had this happen last night and I just fiddled around until it work (shut down tasks, restart VS, changed the DB and file names in the web.config, etc.
So question 1) How do I get out of this state?
But the more important question, how do I prevent myself from getting in this state at all?
The SQL Server Object Explorer window can hold a connection to the database. Closing the window and all windows opened from it releases the connection.
I had the same problem and managed to fix it. The main problem is that EF won't automatically create the database. The solution is not very hard.
First, go to the Solution Explorer and manually create the database. To do this, go to the folder where your database is located (usually App_Data). Right-click on this folder and click on Add -> New Item. Select the Data tab and select SQL Server Database. Make sure you enter the name Entity Framework expects (which is specified in the Cannot attach the file '{0}' as database '{1}' error message).
Then most likely you will get an error message that says that the database does not contain any model metadata. To circumvent this, change your database initialisation to:
Database.SetInitializer(new DropCreateDatabaseAlways<SomeContext>());
Now run the application and the database should be there with tables correctly created. Once this step has been successfully executed, you change change your database initializer back to:
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<SomeContext>());
I'm building a web application which soon I'll be promoting to production. This application has a login screen, as you know I used the integrated DB for developing. Now I want to move this database to a full SQL Server 2005. I'm using the aspnet_regsql.exe to create my membership tables and stored procedures in the SQL Server 2005 but I'm getting an error:
An error occurred during the execution of the SQL file 'InstallMembership.sql'. The SQL error number is 207 and the SqlException message is: Invalid column name 'FailedPasswordAnswerAttemptWindowStart'.
I google this for half of the day already and I can't seem to fine what's wrong, does anybody have ANY ideas on what am I doing wrong here? Any help will be really appreciate it.
The easiest way to deploy a brand new database to a remote server is to just publish the database from within Visual Studio. Once you do that you have the scheme and data all in one script. You then access your server either via a local SQL manager connection or a remote manager and run the script on your remote database.
The site 4GuysFromRolla has a nice little walk through with graphics which describe how to accomplish the publishing of your local database to your hosted one.
Good luck, and hope this helps you out some.
Go through the *.sql script file and see where that column reference is. Then you'll be able to see what exactly the script is doing to cause this error. My guess would be that it is getting caught up on referential integrity. If it is trying to create a foreign key constraint that that table/column does not exist then you'll see that error.
But back to the original question at hand. You don't need to recreate your whole database. That script file is to lay down the Membership for an empty database. If you have a working dev database you can just back it up and restore it to a production instance. No need to rebuild the membership database objects.