C# 如何在多实体数据模型之间共享连接字符串
我有一个具有4个实体数据模型的项目。要构建它们,我不想在项目中保存连接字符串,我想在C# 如何在多实体数据模型之间共享连接字符串,c#,asp.net,linq,entity-framework,c#-4.0,C#,Asp.net,Linq,Entity Framework,C# 4.0,我有一个具有4个实体数据模型的项目。要构建它们,我不想在项目中保存连接字符串,我想在app.config文件中存储连接字符串,并在我的模型之间共享。我如何做到这一点 谢谢您可以先使用代码。您必须在代码中为每个模型编写映射,但是使用相同的连接字符串非常容易——只需将“name=MyConnectionStringName”传递给DbContext构造函数即可 首先将数据库与EDMX一起使用时,EF连接字符串包含: “存储连接字符串”,提供有关如何连接到数据库的信息 描述模型的EF元数据的路径
app.config
文件中存储连接字符串,并在我的模型之间共享。我如何做到这一点
谢谢您可以先使用代码。您必须在代码中为每个模型编写映射,但是使用相同的连接字符串非常容易——只需将“name=MyConnectionStringName”传递给DbContext构造函数即可 首先将数据库与EDMX一起使用时,EF连接字符串包含:
- “存储连接字符串”,提供有关如何连接到数据库的信息
- 描述模型的EF元数据的路径
似乎很难相信编写所有这些非平凡的代码会比在app.config中只有四个连接字符串要好。您可以将EF连接字符串传递给ObjectContext。如果您有多个模型,您可以将每个模型的连接字符串放在app.config中,给每个模型一个键,并在需要时查找,并在实例化时将适当的字符串传递给上下文。假设先使用DbContext和模型/数据库
private static EntityConnection CreateConnection(string connectionString)
{
// Find the name of the shared connection string.
const string appSettingKey = "SharedConnectionStringName";
string sharedConnectionStringName = ConfigurationManager.AppSettings[appSettingKey];
if (string.IsNullOrEmpty(sharedConnectionStringName))
{
throw new Exception(string.Format(
"Shared connection not configured. " +
"Please add a setting called \"{0}\" to the \"appSettings\" " +
"section of the configuration file.", appSettingKey));
}
// Create a (plain old database) connection using the shared connection string.
ConnectionStringSettings backendSettings =
ConfigurationManager.ConnectionStrings[sharedConnectionStringName];
if (backendSettings == null)
{
throw new Exception(string.Format(
"Invalid connection string name \"{0}\" in appSetting \"{1}\"",
sharedConnectionStringName, appSettingKey));
}
System.Data.Common.DbConnection dbConn =
Database.DefaultConnectionFactory.CreateConnection(
backendSettings.ConnectionString);
// Create a helper EntityConnection object to build a MetadataWorkspace out of the
// csdl/ssdl/msl parts of the generated EF connection string for this DbContext.
EntityConnection wsBuilder = new EntityConnection(connectionString);
// Merge the specific MetadataWorkspace and the shared DbConnection into a new EntityConnection.
return new EntityConnection(wsBuilder.GetMetadataWorkspace(), dbConn);
}
您可能需要通过代码传递连接字符串,下面是一个关于它的详细答案,是该代码优先还是使用EDMXmodel@LukeMcGregor:它是
EDMX
model我不是基于EDMX的EF方面的专家,但从我刚才尝试过的情况来看,如果在创建EDMX模型时取消选中save my connection string位,它将使用同一数据源中已经存在的一个下面的场景*您正在为一个数据库编写代码*您在测试数据库上工作,因此需要在生产环境中更改连接字符串
private static EntityConnection CreateConnection(string connectionString)
{
// Find the name of the shared connection string.
const string appSettingKey = "SharedConnectionStringName";
string sharedConnectionStringName = ConfigurationManager.AppSettings[appSettingKey];
if (string.IsNullOrEmpty(sharedConnectionStringName))
{
throw new Exception(string.Format(
"Shared connection not configured. " +
"Please add a setting called \"{0}\" to the \"appSettings\" " +
"section of the configuration file.", appSettingKey));
}
// Create a (plain old database) connection using the shared connection string.
ConnectionStringSettings backendSettings =
ConfigurationManager.ConnectionStrings[sharedConnectionStringName];
if (backendSettings == null)
{
throw new Exception(string.Format(
"Invalid connection string name \"{0}\" in appSetting \"{1}\"",
sharedConnectionStringName, appSettingKey));
}
System.Data.Common.DbConnection dbConn =
Database.DefaultConnectionFactory.CreateConnection(
backendSettings.ConnectionString);
// Create a helper EntityConnection object to build a MetadataWorkspace out of the
// csdl/ssdl/msl parts of the generated EF connection string for this DbContext.
EntityConnection wsBuilder = new EntityConnection(connectionString);
// Merge the specific MetadataWorkspace and the shared DbConnection into a new EntityConnection.
return new EntityConnection(wsBuilder.GetMetadataWorkspace(), dbConn);
}