实体框架可以与运行时给出的连接字符串一起使用吗?

时间:2012-02-02 14:46:06

标签: c# entity-framework

我有一个应用程序是另一个应用程序的扩展。主应用程序具有提供连接字符串的上下文。在过去,我设置了实体框架,第一步是设置连接字符串。

在这种情况下,我无法设置连接字符串,因为直到运行时才有它。我还可以使用实体框架吗?如果是这样,我该如何设置?

6 个答案:

答案 0 :(得分:3)

绝对:

SqlConnection dbConn = new SqlConnection("your_connection_string");
EntityConnection entityConnection = new EntityConnection(dbConn);
var yourEdmx = new DbModel(entityConnection);

答案 1 :(得分:2)

使用EntityConnectionStringBuilde r Class。

EntityConnectionStringBuilder entityBuilder =
    new EntityConnectionStringBuilder();
entityBuilder.Provider = providerName;
entityBuilder.ProviderConnectionString = providerString;

答案 2 :(得分:1)

是的,您可以在创建Context时指定连接。

答案 3 :(得分:0)

您还可以在事实之后设置上下文数据库默认连接工厂:

dbContext.Database.DefaultConnectionFactory = someConnectionString;

答案 4 :(得分:0)

简单方法:

var DB = new Your_Entities();
DB.Database.Connection.ConnectionString = "Your_Connection_String";

答案 5 :(得分:0)

我有一个项目的问题...一切都很好,所有奇迹......当你在计算机中运行应用程序时,如何使数据库连接适应不同的用户?

我在这里做了一切,其他很多帖子与此类似,没有任何接缝可以工作......是的,代码有效,数据上下文捕获连接的新信息,但它根本不工作...... :'(

我找到了解决问题的救生员示例,这是一堆代码,但DBCon包含新连接的信息:

Encripter enc = new Encripter();
string strconfig = u.readFile(u.ubicacionActual() + "\\conn.config");
DBCon dbcon = JsonConvert.DeserializeObject<DBCon>(enc.decrypt(strconfig));

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
EntityConnectionStringBuilder efb = new EntityConnectionStringBuilder(config.ConnectionStrings.ConnectionStrings["ProyectEntities"].ConnectionString);
SqlConnectionStringBuilder sqb = new SqlConnectionStringBuilder(efb.ProviderConnectionString);

// Now we can set the datasource
sqb.DataSource = dbcon.datasource;
sqb.InitialCatalog = dbcon.catalog;
sqb.UserID = dbcon.user;
sqb.Password = dbcon.password;

efb.ProviderConnectionString = sqb.ConnectionString;

// to rewrite the app.config!
ChangeEFConnectionString("ProyectEntities", efb.ProviderConnectionString);

ProyectEntities db = new ProyectEntities(); // dbcontext

神奇的功能(嗯,这不是真正的魔法!只是覆盖app.config

private bool ChangeEFConnectionString(string connStringName, string newValue)
{
    try
    {
        //CreateXDocument and load configuration file
        XDocument doc = XDocument.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
        //Find all connection strings
        var query1 = from p in doc.Descendants("connectionStrings").Descendants()
                     select p;
        //Go through each connection string elements find atribute specified by argument and replace its value with newVAlue
        foreach (var child in query1)
        {
            foreach (var atr in child.Attributes())
            {
                if (atr.Name.LocalName == "name" && atr.Value == connStringName)
                {
                    if (atr.NextAttribute != null && atr.NextAttribute.Name == "connectionString")
                    {
                        // Create the EF connection string from existing
                        EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder(atr.NextAttribute.Value);
                        //
                        entityBuilder.ProviderConnectionString = newValue;
                        //back the modified connection string to the configuration file
                        atr.NextAttribute.Value = entityBuilder.ToString();
                    }
                }
            }
        }
        doc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
        return true;
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
        return false;
    }
}

a Bahrudin Hrnjica

的所有学分