码迷,mamicode.com
首页 > 数据库 > 详细

EF使用自定义字符串连接数据库

时间:2016-08-09 16:12:01      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:

edmx的构造函数:

public TestCheckUpdatesEntities(): base(Config.DataBaseConnectionString(), "TestCheckUpdatesEntities")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }

EF连接字符串模板类: 

public class Config
    {
        public static string DataBaseConnectionString()
        {
            string DataSource = ConfigurationManager.AppSettings["Server"].ToString();
            string DataBase = ConfigurationManager.AppSettings["DataBase"].ToString();
            string UserId = ConfigurationManager.AppSettings["User"].ToString();
            string PassWord = ConfigurationManager.AppSettings["Password"].ToString();
            return ConnectionString(DataSource, DataBase, UserId, PassWord);
        }

        public static string ConnectionString(string DataSource, string DataBase, string UserId, string PassWord)
        {
            return string.Concat("metadata=res://*/ClientData.csdl|res://*/ClientData.ssdl|res://*/ClientData.msl;provider=System.Data.SqlClient;provider connection string=\"",
                "Data Source=" + DataSource + ";Initial Catalog=" + DataBase + ";User ID=" + UserId + ";Password=" + PassWord + ";MultipleActiveResultSets=True;\"");
        }
    }

配置文件:

<appSettings>
    <add key="Server" value="192.168.1.79" />
    <add key="DataBase" value="TestCheckUpdates" />
    <add key="User" value="sa" />
    <add key="Password" value="zblx2880011" />
  </appSettings>

 

EF使用自定义字符串连接数据库

标签:

原文地址:http://www.cnblogs.com/a849788087/p/5753573.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!