.mdf file not able to attach at runtime MVC4 - asp.net-mvc

ERROR:
Cannot attach the file 'C:\Users\dboyle\Desktop\New folder
(2)\OilNGasWeb\OilNGasWeb\App_Data\aspnet-MvcApplication4-20130726115749.mdf' as database 'aspnet-MvcApplication4-20130726115749'.
*This error occured after deleting my Web.config file *( replacing it with a new one from a new project and replacing the default conection string with what i needed)
EDIT:
Sofar I have tried deleting the .mdf physical file and from VS, also from SQL. Then re-running the update from the package manager within VS. The database gets made, yet same error persists.
I have found
<ObjectGroup Name="DefaultConnection" Order="1" Enabled="False">
<Destination Path="Data Source=ANE-SQL\ANESQLSERVER;Initial Catalog=OilGas;User ID=software;Password=GLvp$102" />
<Object Type="DbCodeFirst">
<Source Path="DBMigration" DbContext="OilNGasWeb.Models.OilNGasDB, OilNGasWeb" MigrationConfiguration="OilNGasWeb.Migrations.Configuration, OilNGasWeb" Origin="Configuration" />
</Object>
</ObjectGroup>
in my website.pubxml file ( it still shows my old database name ) will this interfier?

While doing database update using code-first migrations in ASP.Net MVC, came across the strange exception and details are as follows,
Issue back ground details,
Manually deleted auto created ".mdf" file from App_Data folder using Visual Studio.
Executed update-database in package manager console. Then got the below exception,
System.Data.SqlClient.SqlException (0x80131904): Cannot attach the file 'E:\Backup\Practice\MVC4\DotNetExamples\DotNetExamples\App_Data\DotnetExamples.mdf' as database 'DotnetExamples'.
Solution:
If you delete the DB file, it still stays registered with SqlLocalDB. Sometimes it fixes it by deleting DB. We can do this from the command line.
Open the "Developer Command Propmpt for VisualStudio" under your "Start/Programs menu->All Programs->Visual Studio 2012->Visual Studio Tools"
Run the following commands:
sqllocaldb.exe stop v11.0
sqllocaldb.exe delete v11.0
Please have the .mdf file and SSMS instance of the Database Deleted.
Now execute "update-database" command from package manager console and it will create database for you without any obstacles.

You probably have an attached db with the same name in your SQL Server, just open up SQL remove it and run your MVC4 again.

I had the same problem.
I used Visual Studio 2015 and SQL Server LocalDB v12 so the answer from the accepted answer didn't work for me. I used these commands instead (without version) and it worked:
sqllocaldb.exe stop
sqllocaldb.exe delete

Related

Database migration for .Netcore MVC project in Mac

I am just into dotnet and I am finding it difficult as most of the resource is available only for windows.
I have the docker running. When I try to update the database (dotnet ef database update) in the command terminal, it builds and shows an error saying "Connection string keyword 'server' is not supported."
The project is empty and its the default one after creating a new project with MVC
For incorporating Server in the visual studio I made few changes in the app settings.json
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Database=leavemanagementnet6;User=SA;Password=MyPassword123#;"
},
By default, it uses SQL Lite. Under program.cs, it should be changed to UseSqlServer(connectionString).
Also, the default migration has to be removed and created again. If its not done, we will be facing error while updating.
dotnet ef migrations remove
dotnet ef migrations add CreateIdentity -o Data/Migrations

Entity Framework migrate.exe not working after updating to AspNet.Identity

Since updating my solution to use ASPNET Identity instead of the old membership, the migrate.exe commando to update database schema stopped working. The only major change is that my Context now inherits from a IdentityDbContext, when before it inherited from a DbContext.
Everything works fine running update-database on package manager console, but using migrate.exe on the command line doesn't work anymore. I get the error:
System.Data.Entity.Migrations.Infrastructure.MigrationsException: No migrations configuration type was found in the assembly 'SampleProject.Repository.EF'. (In Visual Studio you can use the Enable-Migrations command from Package Manager Console to add a migrations configuration).
I have a Configuration.cs file for the migrations, as always had and when running the "enable-migrations" suggested I get a message saying that migration are already enabled for the project.
Does anyone knows what the problem might be?
Thanks
Just a quick update on the solution.
It was DLL version conflict when running migrate.exe.
I was using the output from my web project (which also contained DLL for the EF project).
I updated it in order to use the bin content of my EF project and it worked just fine.

Run Migrations command if not have visual studio

I'm publishing a website ASP.NET MVC and get this error:
“Migrations is enabled for context ‘Context’ but the database does not exist or contains no mapped tables. Use Migrations to create the database and its tables, for example by running the ‘Update-Database’ command from the Package Manager Console.”
I can't install visual studio on server so i can't run ‘Update-Database’ command.
How to solve this problem ?
Find migrate.exe (YourApp\packages\EntityFramework.6.1.3\tools) and paste it in bin folder on the server.
Open Command Prompt on the server.
Run below commands
cd "C:\Websites\YourApp\bin" migrate.exe YourAppName.Api.dll /startupconfigurationfile:..\Web.config /verbose
Please Note that YourAppName.Api.dll should be the DLL having Enabled Migration.
you do not need to install visual studio on the server.
if you have your database connection string setup in web.config you should be able to run update-database in the package manager console and it will update your database on your web host.

Error Message: TF255235 While manually importing TFS2008 db into TFS2012

While I was upgrading from TFS2008 to TFS2012 I received an error stating:
Upgrade Failure: "The installation and configuration of Team
Foundation Server succeeded, however upgrading the data was
unsuccessful"
I then found that you can not rerun the upgrade wizard. How can you rerun the update?
First and foremost BACK UP YOUR DATABASES! REALLY!
At this point I turned to the tfsconfig import command. (http://msdn.microsoft.com/en-us/library/vstudio/ff407080.aspx)
I specifically ran:
TFSConfig Import /SQLInstance:TFS01 (my server name)
/CollectionName:(Anything you want) /confirmed
But I then got this error message:
Errors:1 Error Message: TF255235: Database TfsVersionControl on TFS01
does not exist but the current operation requires an existing
database.
So not only did the original upgrade not work, it also killed one of my DBs. That's fine because I have a backup. So open up SSMS and kill whatever database is pointing to the "TfsVersionControl.mdf" file. Then kill the actual mdf and ldf files.
Next, restore TfsVersionControl again from the database. At this point, we are reset back to pre-upgrade...
Now for the work around. It is an easy but ugly one. In SSMS make the user that is running TFS (in my case tfsService a sysadmin). That's it.
Go back into the command window and rerun the import. About 20 minutes later viola it worked perfectly.
Make sure that you remove the sysadmin permission from the user after everything is working perfectly.
I hope this helps someone.

How can I let Visual Studio 2012 detect if virtual directories need to be remapped?

My situation:
I am working on an ASP.NET MVC project and debug in IIS-Express. Sometimes when we create a temporary branch, this branch will use the same port for debugging. This means the virtual directory in IIS is the same and I can't run them both at the same time.
How it used to work in vs2010:
This is not really a problem because I don't want to run them at the same time. However, in VS2010, I would get a warning when opening the branch for the first time. It asked me if I wanted to remap the URL. When I later returned to open the trunk, I would get the same warning (See https://stackoverflow.com/a/3093534/210336).
How it doesn't work in vs2012:
Now in VS2012, I don't get the warning anymore. If I forget to manually go to the settings and press "Create Virtual Directory" (This has happend a few times), then when I click run in the branch, it will actually run the trunk. This can be extremely confusing. Especially if I try to debug or the system breaks on an exception. A source file from the trunk will be opened in the branch solution. I then think I'm editting the branch, but I am in fact editting the trunk...
Is there anyway to let VS2012 perform the same check as VS2010?
Or am I doing/understanding something else completely wrong?
VS actually edits the IIS Express configuration file found in one of these paths
%userprofile%\documents\iisexpress\config\applicationhost.config
%userprofile%\my documents\iisexpress\config\applicationhost.config
When leaving it up to VS, I tend to see duplicated and conflicting configurations.
For local development, I prefer to start IIS Express from cmd or powershell script as it does not require attaching (but can be attached) and gives me control over the configuration. I make a copy of the iis config and specify the config file in the iis express command. I have a powershell script that sets the path based on the executing directory so no matter which branch, it is always set to the one I'm working in.
Here is some information about running iis express from the command line:
http://www.iis.net/learn/extensions/using-iis-express/running-iis-express-from-the-command-line
And here is a good resource for running iis express from powershell:
https://blog.differentpla.net/post/UaYcAPDfiVJBAAAC/running-iis-express

Resources