实体框架4.1代码首先不创建表 [英] Entity Framework 4.1 Code First not creating tables

查看:135
本文介绍了实体框架4.1代码首先不创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是EF 4.1,并使用DBContext等创建了一个存储库。连接字符串设置为指向SQL Server 2008 R2 Dev版。

I am using EF 4.1 and have created a repository using DBContext etc. Connection string set to point to a SQL Server 2008 R2 Dev edition.

当我运行调用我的网关的控制台应用程序,这个网关反过来添加一个实体并保存,抛出异常,表示找不到该表。当我查看我的数据库时,创建一个数据库,但除了EmdMetadata之外,还没有自动创建表。

When I run a console app that calls my gateway which in turn adds an entity and saves, an exception is thrown saying it can't find the table. When I look at my db, there is a database created but there are no tables created automatically except EmdMetadata.

我是否缺少某些东西?

标记

推荐答案

要设置自动下拉并创建,您会做这样的事情...

To set the auto drop and create you would do something like this...

public class MyDbContext : DbContext 
{
    public IDbSet<Foo> Foos { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        Database.SetInitializer(new MyDbContextInitializer());

        base.OnModelCreating(modelBuilder);
    }
}

public class MyDbContextInitializer : DropCreateDatabaseIfModelChanges<MyDbContext>
{
    protected override void Seed(MyDbContext dbContext)
    {
        // seed data

        base.Seed(dbContext);
    }
}

这篇关于实体框架4.1代码首先不创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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