Cannot attach the file *.mdf as database in asp.net - asp.net

I am trying to attach a *.mdf file to a SQL Server Express version. But I still have a problem with attach and then I removed a database and create a new but same problem when I use Update-Database in Package Manager Console.
My default connection string is:
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;AttachDBFilename=App_Data\\TVK3.mdf;Database=TVK3;Trusted_Connection=true;MultipleActiveResultSets=true;"
I get this error:
Cannot attach the file 'App_Data\TVK3.mdf' as database 'TVK3'.
I searching on google and trying some advice but nothing helped to me. Thank you for any advice.

Remove:
"AttachDBFilename=App_Data\TVK3.mdf;"

As davidbaumann said below my posts in comments. When i used absolute filename everythings working good. Thank you.

Related

CodeFirstInstallationService usage?

I am new to Nopcommerce and I tried to find a way to update my database after modify some changes. I found this file name CodeFirstInstallationService.cs which is in the Service directory. My question is:
What is the use of this file? Is it only run once in the first installation, or is run everytime I run the server?
If I modify the content after installation, can the server be updated?
Thank you so much!
I tried to find a way to update my database after modify some changes
First of all, nopCommerce doesn't provide migration update from the code anyway. You have to recreate the DB or you have to add it manually. Please check official doc at: Updating an existing entity. How to add a new property
What is the use of this file? Is it only run once in the first
installation, or is run everytime I run the server?
This service contains information that need to add at the time of installation. You can put the debug point and check if being called every time or not.
Hope this helps!

Creating a database project with Visual Studio - can't find database after running?

I am following this tutorial (http://www.asp.net/mvc/overview/getting-started/database-first-development/setting-up-database) in order to create a database-first app with Entity Framework.
I hit "Run" and everything seems to be working, but in the next part of the tutorial (https://www.asp.net/mvc/overview/getting-started/database-first-development/creating-the-web-application), when I try to add a connection, it's not in the drop-down menu in the "Connection Properties" box under "Select or enter a database name".
Which server is the database on? I didn't specify anything about the server when I created the project for the first tutorial, and I figured that it would be on the default LocalDB that comes with Visual Studio (server name: (localdb)\MSSQLLocalDB), but the database isn't there. Any ideas where I should look or what to try?
Maybe use . (a dot) as Server Name?
Open your Web.config file and check your <connectionStrings> this is where the data source of the database will be. As shown:
However, which version of SQL Server do you have? As this can determine what the correct connection string will be.
Hit Windows+R. And run the following:
%LOCALAPPDATA%\Microsoft\Microsoft SQL Server Local DB\Instances
You may have multiple instances, it would be worth checking each one. So it would be:
(localdb)(name of instance)
This article may also be of use to you:
http://rajchel.pl/2015/10/solving-connection-issue-with-sql-server-2014-localdb/
What could be the case is that you have to run the application you created, and then do something to touch the database like creating / registering a user. After registration you can find the database on the specified location in your web.config settings. This was the solution in my case.

ASP.NET MVC4 Code First - 'Cannot attach the file as database' exception

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.

can i use sqlite in appharbor?

I use sqlite in my appharbor app. the data file was in "/App_Data". in my dev env,it works good .But when I deployment it in appharbor, there was a error :"SQLite error no such table: Items" . It seemed all the tables was lost.
and I print the connection string ,It was "Data Source=D:\websites\f3\d2b85\15729\0x0001_PublishedWebsites\ToolBox\App_Data\d3ah.db;" ,seemed fine.
so , what should I do?
I knew it.
the appharbor build my source code and deployment it.
so,in visual studio, go to solution Explorer and find your sqlite data file.
right click it and then click "properties", and then set Build Action To "Content".
check in source code. Done!
Using SQLite on AppHarbor is not recommended because instance storage is not persisted across deploys. There are more details in the FAQ.

cannot open sqlite database from JavaScript

I'm trying to read sqlite database from the JS side which is created/updated by Android Activity or Service.
But when I call window.openDatabase() method the message below is appear on the LogCat, and Query returns 'no such table' error.
physical .db file is there and seems to accessible from within the Java code, but cannot
access from the JavaScript code. Uninstall or Clear data and reinstall the app did no help with this.
I have no clue to solve this error please help & any suggestions are very appreciated.
phonegap sqlite error code=14
I/Database(2138): sqlite returned: error code = 14, msg = cannot open file at line 27205
If you are trying to access the database created by Activity from the Javascript code, the following thread might answer your question:
How do I call window.openDatabase from a webView hosted in an android application?
Here I am assuming that you are working with Phonegap/Cordova android application .
I also faced the same problem , but I found that this problem is arising because Java and JS both are creating DB at different-2 places.So to solve it use this pluginto access DB created by android in Javascript . This plugin access DB from where Java creates it.

Resources