EF code that does not generate tables

I am working on the first EF Code site, and I wrote my classes and context class, the source of which is:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Entity; using MySite.SalesTool.Data.Entities; using System.Data.Entity.ModelConfiguration.Conventions; namespace MySite.SalesTool.Data { public class SalesToolEntities : DbContext { public DbSet<User> Users { get; set; } public DbSet<UserRole> UserRoles { get; set; } public DbSet<Job> Jobs { get; set; } public DbSet<JobAssigner> JobAssigners { get; set; } public DbSet<JobFile> JobFiles { get; set; } public DbSet<JobStatus> JobStatuses { get; set; } public DbSet<AssignedUser> AssignedUsers { get; set; } } } 

The project builds fine, but when I go to start the site, the tables are not created in the database, and I get an error message indicating that the database cannot find any context object that I am trying to access and access, apparently , because at first the code does not generate any of the necessary tables.

Any ideas why it doesn't generate any of the tables at all and doesn't give me any error information?

+8
asp.net-mvc entity-framework ef-code-first
source share
1 answer

Do you have an initialization strategy?

 Database.SetInitializer(new DropCreateDatabaseIfModelChanges<SalesToolEntities>()); 

From what you signed up, it looks like you created the database yourself. Then you need to specify the initialization otherwise the tables and the data will not be added to the database, and querying the database will throw an exception: {"The specified table does not exist. [Sometable]"}

+7
source share

All Articles