entity framework code first not creating tables using VS 2012

asp.net-mvc-4 entity-framework visual-studio-2012

Question

I have a MVC4 Web api project under visual studio 2012 for which I would like to create and manage database using EF 6.0.2 code first. Actually, the solution entails four projects, namely,

  • OnlineShop.Application (User interface)
  • OnlineShop.Model dedicated to POCO classes only
  • OnlineShop.Test for unit tests and
  • OnlineShop.Core having UnitOfWork, GenericRepository and DatabaseContext classes

Each of the mentioned projects includes an app.config which holds Same connection as that of web.config.

Problem: After running the project (in debug mode), the database is created as it's shown on server explorer (in visual studio 2012). But, it has no tables!!!

The database context class is also as the following:

 public class DatabaseContext : DbContext
{
    public DatabaseContext() : base("OnlineShopDb")
    {
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<DatabaseContext, Configuration>());
    }

    //public static void InitializeDatabase()
    //{
    //    using (var context = new DatabaseContext())
    //    {
    //        context.Database.CreateIfNotExists();
    //        Database.SetInitializer(new MigrateDatabaseToLatestVersion<DatabaseContext, Configuration>());
    //    }
    //}

    public DbSet<Person> Persons { get; set; }
    public DbSet<Customer> Customers { get; set; }
    public DbSet<User> Users { get; set; }
    public DbSet<Audit> Audits { get; set; }
    public DbSet<RoleGroup> RoleGroups { get; set; }
    public DbSet<Role> Roles { get; set; }
    public DbSet<UserInRole> UserInRoles { get; set; }
    public DbSet<Order> Orders { get; set; }
    public DbSet<OrderDetail> OrderDetails { get; set; }
    public DbSet<Product> Products { get; set; }
    public DbSet<ProductCategory> ProductCategories { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
        modelBuilder.Entity<Person>().Map<User>(c => c.Requires("Discriminator").HasValue("User"));
    }
}

I have surfed the web so much, found lots of articles ans samples regarding EF code first approach but unfortunately couldn't handle this bad problem!

I would appreciate it if anyone could help me on this.

1
6
2/20/2014 4:46:01 PM

Accepted Answer

Entity Framework code first does not create your database until you try to access it, so run your project and try to get or insert some record to your database.

another usefull tip is that if you change your models EF by default will throw an exeption, and you will have to drop the current database so EF can crate a new one.

or I recomend you to change that default behavior this way

10
5/23/2017 12:17:31 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow