7

i'm trying do some Entity Framework Code First programming to an existing database .. but I keep seeing this code in my Sql Profiler :-

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

What the hell is this EdmMetadata table and why do is my EF code trying to grab the Id and ModelHash from there?

Remember, I'm trying to work against an existing DB.

Cheers :)

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647

1 Answers1

10

There is no Code-First against existing database. If you have database you are doing Database-first. In such case your mapping is driven by database.

EdmMetadata table keeps hash of current code-first model and it allows DbContext detecting changes of model so that database can be recreated. This feature is turned on by default. You can turn it off by removing convention in OnModelCreating:

modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • 9
    "There is no Code-First against an existing database". er .. are you sure?? The EF team says otherwise: "*This walkthrough is going to demonstrate Code First generating the database schema but the same principals apply to mapping to an existing database, with the exception of ‘7.*" http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-code-first-walkthrough.aspx – Pure.Krome Mar 21 '11 at 23:51
  • 1
    I didn't mean that fluent mapping doesn't work with existing db. It does but it is not code first. Code first means that you create pocos and mapping first and database is generated from these informations. – Ladislav Mrnka Mar 22 '11 at 06:22
  • 2
    It's a wash for me...you were being overly semantic in your first statement (-1). But your second statement was spot on (+1) – Michael Brown Dec 02 '11 at 16:04
  • 2
    Hm, this all makes sense and I added that line to my OnModelCreating method, but in LINQPad I'm still seeing the EdmMetadata table being queried... – Ocelot20 Feb 24 '12 at 14:55