简体   繁体   English

使用Entity Framework CodeFirst从Web.config中删除ConnectionString

[英]Remove ConnectionString from Web.config using Entity Framework CodeFirst

Transform web.config won't work well for Entity Framework CodeFirst because the connection string is added dynamically after transform when publishing. 转换web.config对于Entity Framework CodeFirst不起作用,因为连接字符串是在发布时在转换后动态添加的。 We have a dynamic connection settings being set in code for CodeFirst when releasing to different environments. 在释放到不同环境时,我们在CodeFirst的代码中设置了动态连接设置。

I have tried the correct removal method but the published results ends up with the connection string in the web.config . 我尝试了正确的删除方法,但发布的结果最终以web.config的连接字符串结束。

Are there any work arounds for this? 这有什么工作吗? Putting connection info in our web.config s isn't an option. 在我们的web.config放置连接信息不是一个选项。 Adding the connection info dynamically at runtime works well. 在运行时动态添加连接信息效果很好。

The problem is when publish adds the connection info in the web.config the app gets a 500 error because codefirst is attempting to use an invalid connection string for the environment that is not sandbox where it was created. 问题是当发布在web.config添加连接信息时,应用程序会收到500错误,因为codefirst正在尝试使用无效的连接字符串来创建不是沙箱的环境。

We are changing that at runtime here and that is working. 我们正在运行时更改它,这是有效的。

public MyAppDataContext()
{
   this.Database.Connection.ConnectionString = OurDynamicConfigSettings.GetSetting("MyAppConnectionString");
}

The codefirst though is attempting to use what is in web.config before we set it dynamically. 但是,在我们动态设置之前,codefirst尝试使用web.config内容。 The fix is to remove the connection info from the web.config. 解决方法是从web.config中删除连接信息。

It all works except when building on build server or publishing, codefirst inserts the connection info back into the web.config every time we publish. 这一切都有效,除非在构建服务器或发布时构建,每次我们发布时,codefirst都会将连接信息插回到web.config中。 Having to remember to remove this each time is not good practice and prone to errors if you forget. 必须记住每次都删除这个不是好的做法,如果你忘了就容易出错。

Code for removing connection string in our transform file should work but doesn't. 在我们的转换文件中删除连接字符串的代码应该可以工作但不能。

<connectionStrings>
    <add xdt:Transform="Remove" xdt:Locator="XPath(configuration/connectionStrings[@name='MyAppConnectionString'])" />
</connectionStrings>

Your transform should be: 你的转变应该是:

<connectionStrings>
    <add xdt:Transform="Remove" xdt:Locator="Match(name)" name="MyAppConnectionString" />
</connectionStrings>

But, I have to ask.. are connection strings stored in your database or something? 但是,我要问..你的数据库中存储的连接字符串是什么? Why do you need to do them in code? 为什么需要在代码中执行它们?

You could just do this: 你可以这样做:

public class MyDataContext : DbContext {
     public MyDataContext(string connectionString) : base(connectionString)

   ...
}

Then when you create you contexts, you do this: 然后,当您创建上下文时,执行以下操作:

using(var context = 
   new MyDataContext(OurDynamicConfigSettings.GetSetting("MyAppConnectionString"))) {
    ...
}
 public partial class MyContextEntities : DbContext
    {
        public MyContextEntities(string ConnectionString="[Your Entity Connection String Here]")
            : base(ConnectionString)
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }
}

Replace " with ' (single quote) in connection string 在连接字符串中替换“with”(单引号)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM