EF Code First Not Generating Tables

被刻印的时光 ゝ 提交于 2020-01-23 05:38:05

问题


I'm working on an EF Code first site, and I've written my classes and a 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 run the site, no tables are created in the database and I get an error stating that the database can't find whichever context object I try and access, presumably because the code first has not generated any of the necessary tables.

Any ideas why it wouldn't generate any of the tables at all, and not give me any kind of error information?


回答1:


Do you have an initialization strategy?

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

From what you subscribe it sounds like you've created the database yourself. Then you need to specify an initialization strategy otherwise no tables/data will be added to the database and querying the database will result in an exception: {"The specified table does not exist. [ sometable ]"}



来源:https://stackoverflow.com/questions/6281447/ef-code-first-not-generating-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!