EF code先不要生成表 [英] EF Code First Not Generating Tables

查看:139
本文介绍了EF code先不要生成表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的EF code第一的网站,我写我的课和上下文类,它的来源是:

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; }
    }
}

该项目建立很好,但是当我去跑工地,在数据库中创建没有表,我得到一个错误,指出该数据库无法找到任何上下文对象我尝试访问,presumably因为在code首先还没有产生的任何必要的表。

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?

推荐答案

你有一个初始化的策略?

Do you have an initialization strategy?

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

从你订阅这听起来像你自己创建的数据库。然后,你需要指定一个初始化
策略否则没有表/数据将被添加到数据库和查询数据库将导致异常:{指定的表不存在[sometable]}

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 ]"}

这篇关于EF code先不要生成表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆