Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF 4.1 OnModelCreating not called

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.

like image 393
ASR Avatar asked Oct 24 '11 13:10

ASR


People also ask

How do you call OnModelCreating again?

So to get OnModelCreating fired again IDbModelCacheKeyProvider. CacheKey must be changed. To change cache key, DbContext class must implement IDbModelCacheKeyProvider. When new cache key is returned In CacheKey property of IDbModelCacheKeyProvider then OnModelCreating event fires again.

How often is OnModelCreating called?

OnModelCreating will be called only once that's default behaviour. According to OnModelCreating documentation. Typically, this method is called only once when the first instance of a derived context is created. The model for that context is then cached and is for all further instances of the context in the app domain.

What is OnModelCreating?

The DbContext class has a method called OnModelCreating that takes an instance of ModelBuilder as a parameter. This method is called by the framework when your context is first created to build the model and its mappings in memory.

Is OnModelCreating required?

OnModelCreating is not necessary, but at the same time will not hurt if called - that's why Sometimes it's there, sometimes not. Sometimes at the beginning of the method, other times at the end.


1 Answers

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" />
like image 139
Ladislav Mrnka Avatar answered Oct 08 '22 04:10

Ladislav Mrnka