1

I am using EntityFramework 5.0 and I have a database that already exists but I want to define my own model for it so I do something like this:

pubilc class MyContext : DbContext
{
    public MyContext()
    {
        Whatevers = Set<Whatever>();
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //.. Deal with all the mappings between the real database and
        // my beautiful model
    }

    pubilc DbSet<Whatever> Whatevers { get; private set;}
}

public class Whatever
{
   //... you get the gist...
}

It works fine except for the fact that the first time I start up the application it attempts to execute these two queries:

SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
    COUNT(1) AS [A1]
    FROM [dbo].[__MigrationHistory] AS [Extent1]
)  AS [GroupBy1]

SELECT TOP (1)
[Extent1].[Id] AS [Id],
[Extent1].[ModelHash] AS [ModelHash]
FROM [dbo].[EdmMetadata] AS [Extent1]
ORDER BY [Extent1].[Id] DESC

Now, neither of those tables exist and therefore I get errors, and I really don't want them to (this is a database that has been around for a while and I am just a lowly application developer not a database overlord).

I saw this question: How to disable migration in Entity Framework 4.3.1? that suggested this:

Database.SetInitializer<YourContextType>(new CreateDatabaseIfNotExists());

But according to MSDN that is the default, in version 5.0 at least, anyway.

I see in MSDN that there are also:

  • DropCreateDatabaseAlways
  • DropCreateDatabaseIfModelChanges
  • MigrateDatabaseToLatestVersion

But none of those sound like what I want.

Is there a "leave the database alone - it doesn't know about the entity framework and, quite honestly, doesn't care to know about it" IDatabaseInitializer implementation somewhere?

Is it possible to make one?

Or is there some other property I can tweak - I could not find the AutomaticMigrationsEnabled property mentioned in the answers to the question anywhere.

Or, perhaps there is a crucial reason why I actually need to have those tables in the database?

Community
  • 1
  • 1
kmp
  • 10,535
  • 11
  • 75
  • 125

2 Answers2

3

You could try

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
}

Or

Database.SetInitializer<MyContext>(null);
MichaelLake
  • 1,735
  • 14
  • 17
  • Thanks for the answer. Sadly, if I do `modelBuilder.Conventions.Remove();` it tells me that it is obsolete (and makes no difference anyway) and doing `Database.SetInitializer(null);` makes no difference. – kmp Aug 26 '13 at 09:43
  • So I just tried the SetInitializer(null) thing using EF 6 and it worked! So either it did not work in EF5 or I was being a chump (probably the second), sorry. – kmp Oct 30 '13 at 13:08
  • Database.SetInitializer(null); should be in constructor of context class. – Michał Kuliński Jan 11 '17 at 15:25
1

I think that in this case Reverse Engineer Code First is the way to go.

You will need Entity Framework Power Tools Beta 3 Further explained here: Code First to an Existing Database

As far as i know this implements no migration or what so ever.

Pim
  • 177
  • 1
  • 8
  • Sadly, that generates the same code that I am already writing by hand and, therefore, the two queries are still executed. – kmp Aug 06 '13 at 08:35