如何在EF中禁用代码优先功能(MVC4 Visual Studio 2012) [英] How to disable code-first feature in EF (MVC4 Visual Studio 2012)

查看:141
本文介绍了如何在EF中禁用代码优先功能(MVC4 Visual Studio 2012)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在EF(Visual Studio 2012)中禁用代码优先功能

How to disable code-first feature in EF (Visual Studio 2012)

我正在使用Visual Studio 2012,MVC4(Internet应用程序模板)。

I am using Visual Studio 2012, MVC4 (Internet application template).

我想使用EF,但不是使用其代码优先功能。我希望应用程序出错,而不是根据我的代码创建或修改我的数据库。 (我只是不能忍受我的数据库的这种感觉在幕后改变...我想要应用程序使用我已经创建的确切数据库...如果有任何事情必须改变,我会这样做我的自我)

I want to use EF, but not with its code-first feature. I would want the application to error out, rather than create or modify my database based on my code. (i just can not live with this feeling of my database being changed behind the scenes ... i want the application to use the exact db i have created ... and if there is any thing that has to be changed, i'll do it my self)

这可能与新的Ef(VS2012)?

is this possible with the new Ef (VS2012)?

我看到很多人们问这个问题,但到目前为止我无法找到答案。

i have seen many people asking this, but so far i am unable to find the answer.

推荐答案

您可以使用Code First,确保您的数据库通过将数据库初始化程序设置为 null 更改模型时,永远不会更新或覆盖:

You can use Code First and ensure that your database never gets updated or overwritten when you change your model by setting the database initializer to null:

Database.SetInitializer<MyDbContext>(null);

这是数据库类的静态方法并且应该在应用程序的开始处被调用,例如在global.asax中或在上下文类的静态构造函数中。这样做,您必须手动更改模型类和数据库模式,以使它们匹配。

It's a static method of the Database class and should be called at the beginning of your application, for example in global.asax or in a static constructor of your context class. Doing this you have to change model class and database schema manually so that they match.

您还可以使用反向工程师功能从现有数据库创建代码优先模型。这里有解释: http://msdn.microsoft.com/en-us/data/ jj200620

You can also use the Reverse Engineer feature to create a Code First model from an existing database. It is explained here: http://msdn.microsoft.com/en-us/data/jj200620

或者如果您不想使用Code First,可以使用模型设计器,您可以使用Database First方法,在此解释: http://msdn.microsoft.com/en-us/data/jj206878

Or if you don't want to use Code First at all and work with a model designer you can use the Database First approach, explained here: http://msdn.microsoft.com/en-us/data/jj206878

有关所有可能的选项的概述如下: http://msdn.microsoft.com/en-us/data/ee712907.aspx

An overview about all the possible options is here: http://msdn.microsoft.com/en-us/data/ee712907.aspx

这篇关于如何在EF中禁用代码优先功能(MVC4 Visual Studio 2012)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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