Where is the Database?
I spent more time than I should have trying to troubleshoot why my database not being created. My DbContext-based class looked like:
public class TakeNoteContext : DbContext { public TakeNoteContext() : base("name=TakeNote") { } public DbSetPosts ; public DbSet Users; public DbSet PostTypes; public DbSet SourceTypes; protected override void OnModelCreating(DbModelBuilder modelBuilder) { //base.OnModelCreating(modelBuilder); //an alternative to using attributes modelBuilder.Configurations.Add (new PostConfiguration()); } }
Everything looked right to me, initially. And later I noticed that I was using fields and properties for my DbSet data members.
The correct way:
public class TakeNoteContext : DbContext { public TakeNoteContext() : base("name=TakeNote") { } public DbSetPosts { get; set; } public DbSet Users { get; set; } public DbSet PostTypes { get; set; } public DbSet SourceTypes { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { //base.OnModelCreating(modelBuilder); //an alternative to using attributes modelBuilder.Configurations.Add (new PostConfiguration()); } }
The interesting thing is that the database can be created if I call ctx.Database.CreateIfNotExists(); directly even if I used fields. There might other reasons why your database is not created. My sure your connection is correct, for example.
No comments:
Post a Comment