停止实体框架修改数据库 [英] Stop Entity Framework from modifying database

查看:114
本文介绍了停止实体框架修改数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始玩实体框架的代码优先方法,主要是为了使我可以用我的视图中的注释来装饰我的属性(否则,现在我必须创建一个几乎是与实体框架为我生成的一样,所以我可以添加注释,然后将数据从一个对象复制到下一个对象)。



现在看起来像当我启动我的应用程序时,它正在尝试创建一个数据库。



我不希望实体框架永远修改我的数据库。不,没有。甚至没有尝试。修改数据库真的不是那么难;如果我自己做的话,我会感觉更舒服我不需要框架设计数据库时握住我的手。



我可以告诉框架停止尝试修改我的数据库吗?我非常犹豫使用代码优先,因为它试图修改我的数据库是相当可怕的事实。即使在发展中,我也不想看到它发生。



我没有运气吗?

解决方案

如果您不希望EF创建数据库,可以禁用数据库初始化程序:

  public class SchoolDBContext:DbContext 
{
public Sc​​hoolDBContext():base(SchoolDBConnectionString)
{
//禁用初始化器
Database.SetInitializer&SchoolDBContext> );
}
public DbSet< Student>学生{get;组; }
public DbSet< Standard>标准{get;组; }
}

请参阅 http://www.entityframeworktutorial.net/code-first/turn-off-database-initialization-in -code-first.aspx


I'm starting to play around with the code-first approach to the entity framework, primarily so that I can decorate my properties with annotations for display in my view (otherwise, right now I have to create a class that is nearly identical to the one that entity framework generated for me just so I can add annotations, and then copy the data from one object to the next).

Right now it looks like when I start my application it is trying to create a database.

I do not want entity framework to ever modify my database. No. Not ever. Don't even try it. It really isn't that hard to modify databases; I would feel much more comfortable if I did that myself. I don't need a framework to hold my hand when designing a database.

Can I tell the framework to stop trying to modify my database? I'm very hesitant to use code-first now as the fact that it's trying to modify my database is rather frightening. Even in development I never want to see it happen.

Am I out of luck?

解决方案

If you don't want EF to create your database, you can disable the database initializer:

public class SchoolDBContext: DbContext 
{
    public SchoolDBContext() : base("SchoolDBConnectionString")
    {            
        //Disable initializer
        Database.SetInitializer<SchoolDBContext>(null);
    }
    public DbSet<Student> Students { get; set; }
    public DbSet<Standard> Standards { get; set; }
}

See http://www.entityframeworktutorial.net/code-first/turn-off-database-initialization-in-code-first.aspx

这篇关于停止实体框架修改数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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