Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable automatic table creation in EF 5.0?

I installed Entity Framework 5.0 RC for Framework 4.0 in my project. But when I try to get data from Views I get error. EF tries creating table for this entity.

like image 529
Hryhorii Avatar asked May 23 '12 18:05

Hryhorii


People also ask

Does Entity Framework create tables?

If you're using a Code First approach then Entity Framework will build the table for you. It looks like you are not using Code First, so you will have create the table in the database.

Can I use EF core without migration?

Hi @PanagiotisKanavos, as you mentioned, it does not require migration.


2 Answers

Use this on your application startup to turn off database initialization and migrations:

Database.SetInitializer<YourContextType>(null);
like image 110
Ladislav Mrnka Avatar answered Sep 24 '22 01:09

Ladislav Mrnka


If you want to turn off database initialization/migration completely regardless of in which project you're using your Context you can add a static constructor to your context to call the initializer. This ensures that the SetInitializer will be called once prior to the first construction/use of your context.

public class YourContext : DbContext
{
    static YourContext()
    {
        // don't let EF modify the database schema...
        Database.SetInitializer<YourContext >(null);
    }

    public YourContext() : base("name=YourContext")
    {}
    ...
}

However, if you only want to do this in a select few projects, you're better off doing it explicitly via application startup - e.g. during your normal IoC setup, like suggested by Ladislav.

like image 41
Adriaan de Beer Avatar answered Sep 23 '22 01:09

Adriaan de Beer