English 中文(简体)
如何禁用5. 0 EF 中的自动创建表格?
原标题:How to disable automatic table creation in EF 5.0?

我在我的项目中为框架4.0安装了实体框架5.0 RC 。 但是当我试图从视图中获取数据时, 我得到错误。 EF 尝试为这个实体创建表格 。

最佳回答

在启动应用程序时使用此选项关闭数据库初始化和迁移 :

Database.SetInitializer<YourContextType>(null);
问题回答

If you want to turn off database initialization/migration completely regardless of in which project you re using your Context you can add a static constructor to your context to call the initializer. This ensures that the SetInitializer will be called once prior to the first construction/use of your context.

public class YourContext : DbContext
{
    static YourContext()
    {
        // don t let EF modify the database schema...
        Database.SetInitializer<YourContext >(null);
    }

    public YourContext() : base("name=YourContext")
    {}
    ...
}

然而,如果您只想在选定的几个项目中这样做,您最好通过应用程序启动来明确这样做,例如,在您正常的 IoC 设置期间,如Ladislav 所建议的那样。





相关问题
EF 5.0 Enums Not Generating

BACKGROUND I m using VS 2010 on a machine where I installed .Net 4.5 which I ve read was an in-place install (overrode the .net 4.0 version). I have projects still targeting 4.0 and 4.5 option is not ...

EF5 众多关系实体

我们正在首先使用EF5的预发版本,并采用代号。 我有订单模式和产品模式(many<>many relationship)。 I 创立了以订单信息、产品信息及技术为基础的程序模式。

Entity Framework and SQL Server 2012 Paging

SQL Server 2012 introduces a more efficient mechanism for paging using FETCH and OFFSET which could have a big impact on performance of apps which use a lot of paging. Does Entity Framework 5 support ...

Can I store enums as strings in EF 5?

We have been using EF CF for a while in our solution. Big fans! Up to this point, we ve been using a hack to support enums (creating an extra field on the model; ignore the enum durring mapping; and ...

数据库中没有绘制地图的表格

我正在尝试利用实体框架5实施保存模式。 我已经走到了工作的基础。 我的存放处只是在必要时才装上,数据正在插入。

Entity Framework 5 Enum Naming

I am using EF 5 with migrations and code first. It all works rather nicely, but there are some issues/questions I would like to resolve. Let s start with a simple example. Lets say I have a User ...

热门标签