EF 4代码首先:与现有的表 [英] EF 4 Code First: with existing tables

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

问题描述

我在我的数据库中有一个现有的表,我想使用Code First / Fluent API创建一些额外的表(无论你想要分类它)。以下是我的 DbContext

  public class MyContext:DbContext 
{
public DbSet< Entity1>实体1 {get;组; }
public DbSet< Entity2> Entties2 {get;组;

public MyContext():base(@data source = ....;)
{}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer(new MyContextInitializer());
modelBuilder.Conventions.Remove< IncludeMetadataConvention>();

modelBuilder.Configurations.Add(new Entity1Configuration());
modelBuilder.Configurations.Add(new Entity2Configuration());
}
}

在这里经历一些问题和一些博文我发现以下建议创建不存在的表:

  public class MyContextInitializer:CreateDatabaseIfNotExists< MyContext> 
{
protected override void Seed(MyContext context)
{
context.Database.ExecuteSqlCommand(_mainTable);
context.Database.ExecuteSqlCommand(_relationTable);
base.Seed(context);
}
private readonly string _mainTable = @create table ...;
private readonly string _relationTable = @create table ...;
}

但是 Seed 方法在 MyContextIntializer 未调用可能是数据库已经存在。如果我在 MyContext 中移动 ExecuteSqlCommand 语句到 OnModelCreating 语句得到以下错误:

 在创建模型时无法使用上下文。 

与SQL Management Studio中直接运行SQL以创建表格相比,我的其他选项是什么?

解决方案

描述



几个月前我做了同样的事情。



您可以将模型映射到现有数据库。
Scott Guthrie博客已经有了这个。



我鼓励您使用 Enity Framework电动工具从现有数据库创建模型和上下文。



更多信息





更新



您必须为DbContext设置初始化程序,不要这么做,你的新的Initializer不会被使用。在应用程序启动时执行以下操作。

  Database.SetInitializer< MyContext>(new MyContextInitializer()); 


I have an existing table in my database and I want to create some additional tables using Code First/Fluent API (whatever way you may want to categorize it). Following is my DbContext:

public class MyContext : DbContext
{
    public DbSet<Entity1> Entities1 { get; set; }
    public DbSet<Entity2> Entties2 { get; set; }

    public MyContext() : base(@"data source=....;")
    {}

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        Database.SetInitializer(new MyContextInitializer());
        modelBuilder.Conventions.Remove<IncludeMetadataConvention>();

        modelBuilder.Configurations.Add(new Entity1Configuration());
        modelBuilder.Configurations.Add(new Entity2Configuration());
    }
}

After going through some questions here and some blog-posts I found following way suggested to create non-existing tables:

public class MyContextInitializer : CreateDatabaseIfNotExists<MyContext>
{
    protected override void Seed(MyContext context)
    {
        context.Database.ExecuteSqlCommand(_mainTable);
        context.Database.ExecuteSqlCommand(_relationTable);
        base.Seed(context);
    }
    private readonly string _mainTable=@"create table ...";
    private readonly string _relationTable=@"create table ...";
}

But the Seed method in MyContextIntializer is not called may be database already exists. If I move ExecuteSqlCommand statements to OnModelCreating statement in MyContext I get following error:

The context cannot be used while the model is being created.

What are my other options than running SQL directly in SQL Management Studio to create tables?

解决方案

Description

I have done the same a few months ago.

You can map your Model to an existing Database. Scott Guthrie has blogged about it.

I encourage you to use the Enity Framework Power Tools to create your models and context from your existing Database.

More Information

Update

You must set the initializer for the DbContext, if you don't do that your new Initializer will not used. Do the following on Application start.

Database.SetInitializer<MyContext>(new MyContextInitializer());

这篇关于EF 4代码首先:与现有的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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