Entity Framework migrate.exe not working after updating to AspNet.Identity - asp.net-mvc

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.

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

EF Core Migrations in Separate Project - Issues

I am not successful in adding ef core migrations in a separate project. I have provided the project structure at the end.
The DbContext class and the migrations are in separate projects such that the former is a class library and the migrations project is a console application. I added a reference to the DbContext project in the migrations project however, I am getting errors while running migration commands
\EF.BlogsDb.Migrations > dotnet ef migrations add InitialCreate --project EF.BlogsDb.Migrations.csproj
No DbContext was found in assembly 'EF.BlogsDb.Migrations'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.
I followed advise from this link but I am not sure of the following.
In which project should i add these lines? is it the migrations project or my web project or the DbContext project?
options.UseSqlServer(
connectionString,
x => x.MigrationsAssembly("MyApp.Migrations"));
Why should i Add a reference to the migrations assembly from the startup assembly?
From which location/folder/project should i run this command?
dotnet ef migrations add NewMigration --project MyApp.Migrations
I can share the source code if you can tell me how to do that.
Thanks in advance for the clarifications one might provide!
Here is my project structure looks like:
The issue is solved using the following command.
dotnet ef --startup-project ../Project.Api/ migrations add InitialModel
Here I am using separate folder for the api and the EF.
Run the above command from the EF project folder
Start up project means referred to the api

sqlpackage.exe - how do I exclude synonyms>

I'm running sqlpackage.exe as park of an automated deployment script creation process, however we have synonyms in the database, which are different depending on the environment (Dev/Test/Live). The problem is that the database project has the synonyms as they are in the Dev environment, but when I run sqlpackage to compare against Test or Live, the synonyms are different and so they get scripted to be dropped and re-added to point to Dev.
I've seen on http://blogs.msdn.com/b/ssdt/archive/2015/02/23/new-advanced-publish-options-to-specify-object-types-to-exclude-or-not-drop.aspx that apparently there's a new parameter "ExcludeObjectType", but when I try running it using that parameter it gives me an error 'ExcludeObjectType' is not a valid argument for the 'Script' action (and I have the latest version of sqlpackage.exe).
Any ideas on what I can do here?
After downloading the latest SSDT for Visual Studio I still had the same issue. Next I downloaded Data-Tier Application Framework (May 2015) and used the new SqlPackage installed at C:\Program Files\Microsoft SQL Server\120\DAC\bin\sqlpackage.exe and the error went away and worked as expected.
Thank you sir! When I created the deployment script in VS, no change detected but when I tried to deploy using sqlpackage I got the error:
* The object [x] already exists in database with a different definition and will not be altered.
After adding the ExcludeObjectTypes switch I got the following error:
* 'ExcludeObjectTypes' is not a valid argument for the 'Publish' action.
But after downloading and installing latest Data-Tier App framework all works as expected with no errors.

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.

.mdf file not able to attach at runtime MVC4

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

Resources