Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnModelCreating is never called

I am starting work with entity framework. Problem is that my OnModelCreating method is never called.

this is my context class:

public class TestContext : DbContext
    {
        public TestContext()
            : base("name=TestDBConnectionString")
        { }

        public DbSet<WorkItem> WorkItems { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
        }
    }

Connection string

 <add name="TestDBConnectionString"
      connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog=TestDB;Integrated Security=true"
      providerName="System.Data.SqlClient"/>

I expect to go into OnModelCreating when I call:

 using (var context = new TestContext())
        {

        }

Where I making mistake?

like image 336
Raskolnikov Avatar asked Oct 12 '15 08:10

Raskolnikov


1 Answers

In my case this was happening because I had left the { get; set; } off my DBSet declaration. Took a while to figure that out!

like image 148
Craig Fisher Avatar answered Oct 01 '22 18:10

Craig Fisher