Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在多实体数据模型之间共享连接字符串_C#_Asp.net_Linq_Entity Framework_C# 4.0 - Fatal编程技术网

C# 如何在多实体数据模型之间共享连接字符串

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元数据的路径

我有一个具有4个实体数据模型的项目。要构建它们,我不想在项目中保存连接字符串,我想在
app.config
文件中存储连接字符串,并在我的模型之间共享。我如何做到这一点


谢谢

您可以先使用代码。您必须在代码中为每个模型编写映射,但是使用相同的连接字符串非常容易——只需将“name=MyConnectionStringName”传递给DbContext构造函数即可

首先将数据库与EDMX一起使用时,EF连接字符串包含:

  • “存储连接字符串”,提供有关如何连接到数据库的信息
  • 描述模型的EF元数据的路径
由于第二部分对于每个模型都是不同的,因此需要有四个不同的连接字符串

如果只希望有一个连接字符串,则需要: -以其他方式存储元数据信息 -编写代码以加载元数据并创建MetadataWorkspace -读取单个存储连接字符串并从中创建连接 -使用连接和MetadataWorkspace创建EntityConnection -使用EntityConnection创建ObjectContext -如果您使用的是DbContext,则使用ObjectContext创建DbContext


似乎很难相信编写所有这些非平凡的代码会比在app.config中只有四个连接字符串要好。

您可以将EF连接字符串传递给ObjectContext。如果您有多个模型,您可以将每个模型的连接字符串放在app.config中,给每个模型一个键,并在需要时查找,并在实例化时将适当的字符串传递给上下文。

假设先使用DbContext和模型/数据库

  • 在app.config文件中保留生成的EF连接字符串不变。正如Arthur所说,它们包含指向EF元数据(csdl/ssdl/msl)的路径。模型设计者在开发过程中也会使用它们
  • 添加名为“SharedConnection”的存储连接字符串。这是生产中唯一需要修改的连接字符串。
  • 创建一个从DbContext派生的基类,并从该类派生所有上下文
  • 在基类中显式创建默认EF连接。然后将其修改为使用共享连接字符串,如下所示:
  • 派生上下文的代码不会更改,除非它们必须从BaseContext继承。下面是一个更健壮的CreateConnection方法。它具有错误处理功能,并以添加应用程序设置为代价从代码中删除共享连接字符串的名称

    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);
    }