Multiple DB Contexts in the Same DB and Application in EF 6 and Code First Migrations

Entity Framework 6 added support for multiple DbContexts by adding the -ContextTypeName and -MigrationsDirectory flags. I just ran the commands in my Package Manager Console and pasted the output below… If you have 2 DbContexts in your project and you run enable-migrations, you’ll get an error (as you probably already know): PM> enable-migrations More than … Read more

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

Add migration with different assembly

All EF commands have this check: if (targetAssembly != migrationsAssembly) throw MigrationsAssemblyMismatchError; targetAssembly = the target project you are operating on. On the command line, it is the project in the current working directory. In Package Manager Console, it is whatever project is selected in the drop down box on the top right of that … Read more

How to delete and recreate from scratch an existing EF Code First database

If I’m understanding it right… If you want to start clean: 1) Manually delete your DB – wherever it is (I’m assuming you have your connection sorted), or empty it, but easier/safer is to delete it all together – as there is system __MigrationHistory table – you need that removed too. 2) Remove all migration … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)