MVC 5 Seed Users and Roles

Here is example of usual Seed approach: protected override void Seed(SecurityModule.DataContexts.IdentityDb context) { if (!context.Roles.Any(r => r.Name == “AppAdmin”)) { var store = new RoleStore<IdentityRole>(context); var manager = new RoleManager<IdentityRole>(store); var role = new IdentityRole { Name = “AppAdmin” }; manager.Create(role); } if (!context.Users.Any(u => u.UserName == “founder”)) { var store = new UserStore<ApplicationUser>(context); var … Read more

EF migration for changing data type of columns

You have a default constraint on your column. You need to first drop the constraint, then alter your column. public override void Up() { Sql(“ALTER TABLE dbo.Received DROP CONSTRAINT DF_Receiv_FromN__25869641”); AlterColumn(“dbo.Received”, “FromNo”, c => c.String()); AlterColumn(“dbo.Received”, “ToNo”, c => c.String()); AlterColumn(“dbo.Received”, “TicketNo”, c => c.String()); } You will probably have to drop the default constraints … Read more

Enable Migrations with Context in Separate Assembly?

This will only work in EF 6, but there was a release that added the -ContextProjectName parameter to the -enable-migrations command. By using this command you could do the following: enable-migrations -ContextProjectName MyProject.MVC -StartUpProjectName MyProject.MVC -ContextTypeName MyProject.MVC.MyContextFolder.MyContextName -ProjectName MyProject This will add migrations to your MyProject project using the context in the MyProject.MVC. You need … 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

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

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

Using Entity Framework (code first) migrations in production

There is a Database Initializer you can use to achieve the migration to latest version on startup (or better, the dbinitializer will kick in on first db access), the MigrateDatabaseToLatestVersion, you use it like that: Database.SetInitializer<ObjectContext>( new MigrateDatabaseToLatestVersion<ObjectContext, Configuration>()); Regarding having one file per migration, if you enable automatic migrations you will find them in … Read more