从ADO.NET构建实体连接字符串

时间:2012-12-02 22:49:57

标签: c# asp.net entity-framework

在我的网站上,我有常规的ADO.NET连接字符串,如: ,

现在Entity Framework将自己的连接字符串添加到同一个数据库。

如何在运行时只使用一个ADO.NET连接字符串并构建实体连接字符串?

1 个答案:

答案 0 :(得分:3)

你可以使用EntityConnectionStringBuilder在运行时动态地生成连接字符串但是你仍然需要metada这个

EntityConnectionStringBuilder entityBuilder =
    new EntityConnectionStringBuilder();
    //Set the provider name.
    entityBuilder.Provider = providerName;

    // Set the provider-specific connection string.
    entityBuilder.ProviderConnectionString = providerString;

    // Set the Metadata location.
    entityBuilder.Metadata = @"res://*/AdventureWorksModel.csdl|
                                res://*/AdventureWorksModel.ssdl|
                                res://*/AdventureWorksModel.msl";
using (EntityConnection conn =
    new EntityConnection(entityBuilder.ToString()))
{
    conn.Open();
    Console.WriteLine("Just testing the connection.");
    conn.Close();
}