将 dbml 中的连接字符串指向 app.config [英] Point connectionstring in dbml to app.config

查看:17
本文介绍了将 dbml 中的连接字符串指向 app.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以将Dbml.designer.cs 中的连接字符串指向app.conf 中的连接字符串吗?我写了下面的代码,它成功地指向了 app.config.

Can I just point the connection string in Dbml.designer.cs to the connectionstring in the app.conf? I wrote the code below which it successfully point to the app.config.

public leDataContext() : 
    base(ConfigurationManager.ConnectionStrings["leConnString"].ToString(), mappingSource)
    {
        OnCreated();
    }

但是,每当我修改表或将表添加到 dbml 中时,它都会开始自动将该代码替换到此

However whenever i modify or add a table into the dbml, it will start to auto replace that code into this

 public leDataContext() : 
            base("Data Source=.\SQLEXPRESS;AttachDbFilename="D:\My Projects\App_Data\le.mdf";Integrated Security=True;Connect Timeout=30;User Instance=True", mappingSource)
    {
        OnCreated();
    }

我扩展了连接"选项.将应用程序设置"设置为 False

I have expanded the "Connection" option. Set "Application Settings" to False

推荐答案

不要手动修改 Dbml.designer.cs 文件,因为编辑/添加表格等时它会被重写.如你所说.而不是将 .dbml 设计器文件的 Connection 属性设置为 None 并添加具有无参数构造函数的部分类:

Don't modify Dbml.designer.cs file manually, because it will be rewritten when you edit/add a table etc. as you said. Instead of this set the Connection property for the .dbml designer file to None and add a partial class with parameterless constructor:

public partial class leDataContext
{ 
   public leDataContext() : 
       base(ConfigurationManager.ConnectionStrings["leConnString"].ToString())
    {
        OnCreated();
    }    
}

这篇关于将 dbml 中的连接字符串指向 app.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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