如何禁用EF code-第一个特征(MVC4的Visual Studio 2012) [英] How to disable code-first feature in EF (MVC4 Visual Studio 2012)

查看:167
本文介绍了如何禁用EF code-第一个特征(MVC4的Visual Studio 2012)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何禁用EF code-第一个特征(的Visual Studio 2012)

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

我使用Visual Studio 2012,MVC4(互联网应用模板)。

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

我想用EF,而与它的code-第一个特征。我希望应用程序的基础上我的code的误差出来的,而不是创建或修改我的数据库。 (我只是不能与我的数据库的这种感觉生活在幕后被改变......我想用我所创建的确切DB应用程序......如果还有就是要改变任何事情,我就这样做我自己)

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首先,确保你的数据库永远不会被更新或重写,当你在数据库初始化设置为<$ C $更改模型C>无效:

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.

您还可以使用逆向工程功能从现有的数据库中创建一个code首先模型。它在此说明:<一href=\"http://msdn.microsoft.com/en-us/data/jj200620\">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首先,用模型设计工作,你可以使用数据库第一种方式,这里解释:<一href=\"http://msdn.microsoft.com/en-us/data/jj206878\">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

有关的所有可能选项的概述是在这里:<一href=\"http://msdn.microsoft.com/en-us/data/ee712907.aspx\">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 code-第一个特征(MVC4的Visual Studio 2012)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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