2

I'm using EF6 (6.1.3) code first and recently noticed that my OnModelCreating override in my DbContext class is not being called for some reason. I was trying to remove ManyToManyCascadeDeleteConvention from the default conventions and it wasn't working for some reason. When I put a breakpoint in OnModelCreating I noticed it was never even getting into that routine.

I checked this question -- EF 4.1 OnModelCreating not called -- and no I do'nt have an edmx file. Straight code first here.

public class DashboardDbContext : DbContext
{
    public DashboardDbContext()
        : base("DashboardDbContext")
    {
        Console.WriteLine("hi world");    // breakpoint DOES get hit here
    }    

    //...

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);    // breakpoint DOESN'T get hit here

        this.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);
        // more stuff...
    }
Community
  • 1
  • 1
Tom
  • 796
  • 7
  • 7
  • It's lazy loaded. Are you accessing your context to force it? – Steve Greene Sep 09 '15 at 16:32
  • Yep, first access triggers the creation (or rather, attempted creation) of the database, and I can see in SQL Profiler various DDL commands being executed. But it's ignoring the `OnModelCreating` override which is supposed to be preventing the cascade deletion clause at the end of the constraint statements. (edit -- typo) – Tom Sep 10 '15 at 10:34
  • 1
    Ugh -- found the answer and it turned out to be unrelated to anything remotely logical. Basically someone had added domain specific DbSets to the (separate) context used for ASP.Net Identiy that was getting instantiated first and was the true source of the errors. So it wasn't that it was ignoring this override -- it was bombing out before getting to this override on objects that should have only existed in this context. – Tom Sep 11 '15 at 10:59

0 Answers0