6

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.

Ali
  • 847
  • 2
  • 13
  • 37
  • 2
    have you tried access your db with c# code? like db.products.tolist()?, entityframework build the database untill you try to get or set some data – bto.rdz Feb 20 '14 at 16:49
  • well.. not really just trying it now may be it helps.. – Ali Feb 20 '14 at 16:54
  • for the record I posted my answer – bto.rdz Feb 20 '14 at 17:01
  • no unfortunately it didn't make any changes – Ali Feb 20 '14 at 17:01
  • it seems fine to me, maybe try removing your constructor and the overrides, then EF will work with their defaults values, and check how it works – bto.rdz Feb 20 '14 at 17:04
  • You know what.. it didn't work as I tried to get a list of product categories.. but when I added a new one.. then tables come up! I am really wondered how this could be reasonable.. but finally I could get ride of it.. Thanks a lot for your help.. I will mark your answer – Ali Feb 20 '14 at 17:10
  • I am facing the same issue.. Everything is alike the given example and I am getting the No SQL table found exception while SaveChanges() is called. i have added the items to both the Sets. IUnitOfWork uow = new ScanEUnitOfWork(); uow.Roles.Add(role1); uow.Roles.Add(role2); uow.Users.Add(newUser1); uow.Users.Add(newUser); uow.Users.Add(newUser2); // commit to DB uow.Commit(); //public void Commit() public void Commit() { DbContext.SaveChanges(); } – Sakky Aug 30 '18 at 12:27

1 Answers1

10

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

Community
  • 1
  • 1
bto.rdz
  • 6,636
  • 4
  • 35
  • 52