Junior
Junior

Reputation: 11990

How to stop migration in Entity Framework 6 once and for all?

I am using Entity Framework 6 with an ASP.NET MVC 5 application. I do NOT want to use code-first approach in my project.

I created a new project and never enabled migrations. I created a new class called MyContext which extends DbContext class. Here is what I have in MyContext constructor

public MyContext()
    : base(ConnectionName)
{
        Database.SetInitializer<MyContext>(null);
}

However, each time I debut the application, I see the following output in my Debug output screen.

SELECT Count(*)
FROM INFORMATION_SCHEMA.TABLES AS t...

and

SELECT 
[GroupBy1].[A1] AS [C1]
FROM ( SELECT 
    COUNT(1) AS [A1]
    FROM [dbo].[__MigrationHistory] AS [Extent1]
    WHERE [Extent1].[ContextKey] = @p__linq__0
)  AS ......

How can I really stop migration in my application?

Upvotes: 1

Views: 3059

Answers (3)

Daniel Lorenz
Daniel Lorenz

Reputation: 4336

The problem is the line

Database.SetInitializer<MyContext>(null);

Needs to be in a static constructor, not an instance constructor! :)

So do this:

static MyContext()
{
        Database.SetInitializer<MyContext>(null);
}

Upvotes: 0

Maximo Dominguez
Maximo Dominguez

Reputation: 2085

If it is true you have not Migrations folder, and no Configuration.cs file, the only thing I think you missed is the database initializer. Even if you are disabling database initialization with Database.SetInitializer<MyContext>(null);, it can be configured using the configuration file:

To disable it using configuration files, add (source):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>    
        <add key="DatabaseInitializerForType MyNamespace.MyDbContext, MyNamespace" 
             value="Disabled" />
    </appSettings>
</configuration>

Upvotes: 3

bilpor
bilpor

Reputation: 3889

there are plenty of examples out there. The first thing you need to do is install the power tools through nuget, then if you already have a DB use the reverse engineer wizard. Also take a look at this if you are starting from scratch: EF DB First

this will create the correct context etc for you.

if your using .net core then take a look at this link

.net core reverse engineer

Upvotes: 0

Related Questions