实体框架的DbContext在Azure的Web角色 [英] Entity Framework DbContext in Azure Web Role

查看:162
本文介绍了实体框架的DbContext在Azure的Web角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我迁移现有Web应用程序(使用实体框架5)到Azure的Web角色。

I am migrating an existing Web Application (using Entity Framework 5) to an Azure Web Role.

数据库连接字符串正在从的web.config ServiceConfiguration感动。*。cscfg 文件。

The database connection string is being moved from the web.config to the ServiceConfiguration.*.cscfg files.

问题是,在自动生成 Model.Context.cs 文件,我的实体类是这样定义的:

The problem is that in the auto-generated Model.Context.cs file, my entities class is defined like this:

public partial class MyEntities : DbContext
{
    public MyEntities()
        : base("name=MyEntities")
    { }

    // DbSets, etc
}

这总是会 MyEntities 的web.config 。我怎么可以重写此构造,这样我可以从 ServiceConfiguration在连接字符串中传递。*。cscfg 文件?

This will always look for MyEntities in the web.config. How can I override this constructor so that I can pass in the connection string from the ServiceConfiguration.*.cscfg file?

我可以从这个类派生的,就像这样:

I could derive from this class, like so:

public class MyCloudEntities : MyEntities
{
    public MyCloudEntities()
        : base(CloudConfigurationManager.GetSetting("MyEntities"))
    { }
}

但后来我不得不改变 MyEntities 的每一个实例在code基地,它不会prevent开发者使用 MyEntities 的未来。

But then I have to change every instantiation of MyEntities in the code base, and it wont prevent developers from using MyEntities in the future.

推荐答案

您可以更改Model.Context.tt文件,使用

You can change Model.Context.tt file, to use

CloudConfigurationManager.GetSetting("MyEntities")

代替

"name=MyEntities"

有关MyEntities

for MyEntities

所以每次上下文将被重新创建时,你将永远有您的更改。在这种情况下,你不需要改变任何东西。

So each time when context will be re-created, you will always have your changes. In this case you don't need to change anything else.

这篇关于实体框架的DbContext在Azure的Web角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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