I want to use Entity Framework + ASP Identity but I don't want EF to generate tables for me

asp.net asp.net-identity c# entity-framework entity-framework-6


I have created new ASP MVC 5 app (with asp identity).
I use IdentityDbContext class to put all my domain objects inside.

public class SecurityContext : IdentityDbContext{
public DbSet<Country> Countries { get; set; }

When I first time created user all database tables were created (identity tables) including my Country table.
But now I added new table in database (created from Management Studio) and added in IdentityDbContext:

public DbSet<City> Cities { get; set; }

But when I run the app now I get error:

The model backing the 'SecurityContext' context has changed since the database was created. Consider using Code First Migrations to update the database.

If I delete all tables and run app again City will be created but I wan't to be more flexible.
So what I want to do is that I can create all database tables and to create my POCO classes in project later so I can use EF for data access.
I don't want EF to create classes for me (I feel more secure when connect those things alone).
Also (I know that it is possible but just don't know how) can I prevent asp Identity to create tables alone?

To sum whole question. I want to use EF but I want to create my database myself including Identity tables. How can I do this?

6/28/2014 2:19:30 PM

Accepted Answer

You can turn off the EF initializer and manage the tables and mappings yourself.

Disable the database initializer in the constructor like this...

public SecurityContext()
    : base("DefaultConnection")

Then you can just take the schema that EF has created thus far and manage it yourself (you can remove the migrations history table). I recommend using a SQL Server Database project so that you can source control your schema. It's now up to you to update the DbContext when the schema changes.

6/28/2014 3:37:16 PM

Popular Answer

You can also disable it through configuration

<?xml version="1.0" encoding="utf-8" ?>
    <add key="DatabaseInitializerForType YourNameSpace.SecurityContext, YourLibraryName" value="Disabled" />

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow