EF Migrations: Rollback last applied migration?

I want to add some clarification to this thread: Update-Database -TargetMigration:”name_of_migration” What you are doing above is saying that you want to rollback all migrations UNTIL you’re left with the migration specified. Thus, if you use GET-MIGRATIONS and you find that you have A, B, C, D, and E, then using this command will rollback … Read more

GUID COMB strategy in EF

Why worry about defaults for Guid columns in the database at all? Why not just generate the Guid on the client like any other value. That requires you have a method in your client code that will generate COMB-like guids: public static Guid NewGuid() { var guidBinary = new byte[16]; Array.Copy( Guid.NewGuid().ToByteArray(), 0, guidBinary, 0, … Read more

Mapping Database Views to EF 5.0 Code First w/Migrations

You have specified that the ClientStatisticsView entity should be mapped to a table named “ClientStatistics”. So entity framework will generate a migration containing an instruction to create that table. But you have independently created that view in the database so to prevent the error you are getting you should remove the CreateTable instruction from the … Read more

Possible to default DateTime field to GETDATE() with Entity Framework Migrations?

You can use DateCreated = c.DateTime(nullable: false, defaultValueSql: “GETDATE()”) Usage: public partial class MyMigration : DbMigration { public override void Up() { CreateTable(“dbo.Users”, c => new { Created = c.DateTime(nullable: false, defaultValueSql: “GETDATE()”), }) .PrimaryKey(t => t.ID); … Update 2012-10-10: As requested by Thiago in his comment, I add a little extra context. The code … Read more

Can you create sql views / stored procedure using Entity Framework 4.1 Code first approach

We support stored procedures in our Entity Framework Code First Migrations. Our approach is to create some folder to hold the .sql files (~/Sql/ for example). Create .sql files in the folder for both creating and dropping the stored procedure. E.g. Create_sp_DoSomething.sql and Drop_sp_DoSomething. Because the SQL runs in a batch and CREATE PROCEDURE.. must … Read more

Debug code-first Entity Framework migration codes

I know that EF Code First Migrations is relatively new tool but don’t forget about you are still in .NET. So you can use: if (System.Diagnostics.Debugger.IsAttached == false) { System.Diagnostics.Debugger.Launch(); } After that you can see your InnerException. Or you can use try…catch statement like this: Exception handling Entity Framework

MVC3 and Code First Migrations – “model backing the ‘blah’ context has changed since the database was created”

From my experience that suggests that migration table is out of sync (even if your data isn’t), and that’s been part of the db schema now (since 4.3 I think – under system tables). There could be many reasons and ways to experience that error , but most of the time… The problematic part is … Read more

why remove-migration run my app?

Update for ASP.NET Core 2.1 In ASP.NET Core 2.1 the methods changed slightly. The general method is similar to the 2.0, just the methods name and return types have been changed. public static void Main(string[] args) { CreateWebHostBuilder(args) .Build() .Migrate(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) { return new WebHostBuilder() …; // Do not call … Read more

There is already an object named in the database

it seems there is a problem in migration process, run add-migration command in “Package Manager Console”: Add-Migration Initial -IgnoreChanges do some changes, and then update database from “Initial” file: Update-Database -verbose Edit: -IgnoreChanges is in EF6 but not in EF Core, here’s a workaround: https://stackoverflow.com/a/43687656/495455