16

I have a problem with EF 4.1 not calling OnModelCreating so that I can configure tables etc. I have an existing database. Here is my connection string:

<add name="AcmeDBContext"
     connectionString="metadata=res://*/|res://*/|res://*/;
                       provider=System.Data.SqlClient;
                       provider connection string=&quot;
                       data source=[server];
                       initial catalog=Acme;integrated security=True;
                       multipleactiveresultsets=True;App=EntityFramework&quot;"
     providerName="System.Data.EntityClient" />

Here is my class inherited from DbContext:

public class AcmeDBContext : DbContext
{
  public AcmeDBContext()
    : base()
  {
    Database.SetInitializer<AcmeDBContext>(null);        
  }

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
    modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();       
    modelBuilder.Entity<Vehicle>().ToTable("tblVehicle");
    modelBuilder.Entity<VehicleMake>().ToTable("tblVehicleMake");
    modelBuilder.Entity<VehicleModel>().ToTable("tblVehicleModel");
    base.OnModelCreating(modelBuilder);
  }

 }

I have read and read online, but I cannot point out what the problem is. The OnModelCreating is never called and hence the exceptions saying that the entity/classes (Vehicle, VehicleMake, VehicleModel) do not exist in the current context when I try to query anything.

Nikolai Samteladze
  • 7,699
  • 6
  • 44
  • 70
ASR
  • 429
  • 3
  • 6
  • 16

1 Answers1

23

You are using two approaches together - your connection string says that you want to use model first or database first with EDMX but you are writing context to use code first / fluent api to map database. Once you use EDMX OnModelCreating is never called. Use common SQL connection string without metadata resources if you want to use OnModelCreating.

<add name="AcmeDBContext"
     connectionString="data source=[server];
                       initial catalog=Acme;integrated security=True;
                       multipleactiveresultsets=True;App=EntityFramework"
     providerName="System.Data.SqlClient" />
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • So If I want to use DataBase First( Don't think I got a choice ), then where do I map class names to table names? Do I have to use classnames same as table names?? I am sorry I have used NH before, but not EF, and I could easily map classes to tables with FluentNH – ASR Oct 24 '11 at 14:10
  • I removed metadata information and still no change in behaviour!! OnModelCreating still not getting called. – ASR Oct 24 '11 at 14:13
  • If you want to use Database First you create an EDMX file. You have the option to create this from an existing database. After the first import you will have a basic entity model which you can completely modify to your needs. The entity framework then performs the mapping from your entity model to the database schema. See http://msdn.microsoft.com/en-us/data/ff191186 – Wouter de Kort Oct 24 '11 at 14:15
  • `OnModelCreating` is called first time the database is used. It is not called automatically when the application start. If you want to use fluent mapping using `OnModelCreating` is one way to specify mapping between your tables and classes. – Ladislav Mrnka Oct 24 '11 at 14:26
  • OK I guess I had to create a edmx file and change the table names in edmx designer. Tx a lot for help. Much appreciated. – ASR Oct 24 '11 at 16:55
  • @LadislavMrnka I want to use Model-First and create a database initializer, the OnModelCreated is never called. I changed the connection string like you described above, but it's still not called. – Shimmy Weitzhandler May 02 '12 at 02:38
  • @Shimmy: You cannot use model first and an initializer. By switching the connection string to the one mentioned in the answer you will switch to code first. The method is called only if you use the context for data retrieval or persistance for the first time. – Ladislav Mrnka May 02 '12 at 05:48
  • @LadislavMrnka, the truth is, I don't care whether you call it model-first or code-first, All I need the designer is so I don't have to write the entities/context manually and it's generated automatically. So does this changes anything? I changed the connection string to: `` and I get the following `ArgumentException`: `Keyword not supported: 'data source'.`, I believe the problem is an improper connection string. Can you please help me? – Shimmy Weitzhandler May 02 '12 at 13:17
  • 1
    @Shimmy - for completeness' sake even though it's a three years old comment: "the truth is I don't care whether you call it model-first or code-first" - these are two completely different concepts in Entity Framework. It's close to saying "I don't care whether you call it NHibernate or Entity Framework". You need to decide which you want to use. – chiccodoro Mar 05 '15 at 11:54