更改nopCommerce的连接字符串?

时间:2015-04-06 15:06:50

标签: connection-string nopcommerce

我正在使用nopCommerce,我需要删除settings.txt文件中的连接字符串并插入web.config文件。我怎么能这样做?

5 个答案:

答案 0 :(得分:3)

将连接字符串从settings.txt移到web.config中的最简单方法是修改Nop.Core.Data.DataSettingsManager。特别是LoadSettings()SaveSettings()方法。您可以将连接字符串存储在您想要的任何位置(理想情况下在web.config中),只要这两个方法读取和写入配置即可。

可以在此Gist中找到更新以支持在web.config中存储连接字符串的DataSettingsManager的粗略示例:http://git.io/vUPcI只需将settings.txt中的连接字符串复制到web.config和命名连接" DefaultConnection"或相应调整代码。

答案 1 :(得分:2)

只需做两步

  1. LoadSettings中替换两个方法SaveSettings\nopCommerce\Libraries\Nop.Core\Data\DataSettingsManager.cs。来自@Stephen Kiningham链接的代码

    /// <summary>
    /// Load settings
    /// </summary>
    /// <param name="filePath">File path; pass null to use default settings file path</param>
    /// <returns></returns>
    public virtual DataSettings LoadSettings(string filePath = null)
    {            
        try
        {
            System.Configuration.Configuration webConfig = WebConfigurationManager.OpenWebConfiguration(HttpRuntime.AppDomainAppVirtualPath);
            return new DataSettings
            {
                DataConnectionString = webConfig.ConnectionStrings.ConnectionStrings["DefaultConnection"].ConnectionString,
                DataProvider = webConfig.ConnectionStrings.ConnectionStrings["DefaultConnection"].ProviderName
            };
        }
        catch (NullReferenceException)
        {
            return new DataSettings();
        }
    }
    
    /// <summary>
    /// Save settings to a file
    /// </summary>
    /// <param name="settings"></param>
    public virtual void SaveSettings(DataSettings settings)
    {            
        if (null == settings) throw new ArgumentNullException("settings");
    
        System.Configuration.Configuration webConfig = WebConfigurationManager.OpenWebConfiguration(HttpRuntime.AppDomainAppVirtualPath);
    
        webConfig.ConnectionStrings.ConnectionStrings["DefaultConnection"].ConnectionString = settings.DataConnectionString;
        webConfig.ConnectionStrings.ConnectionStrings["DefaultConnection"].ProviderName = settings.DataProvider;
    
        webConfig.Save();
    }
    
  2. 将连接字符串添加到您的Web配置web.config

    <connectionStrings>
        <add name="DefaultConnection"
             connectionString=" Data Source=localhost;Initial Catalog=nopcommerce;Integrated Security=True;Persist Security Info=False"
             providerName="sqlserver">
        </add>
    </connectionStrings>
    

答案 2 :(得分:1)

除了将连接添加到web.config之外,还必须指定providerName =“sqlserver”。

  

Ex);初始目录=;综合   安全性=假;用户ID =;密码=;连接   超时= 30;加密=真”            providerName =“sqlserver”/&gt;

这是因为Nop.Data中的EfDataProviderManager检查了提供者名称,如果放入正常的话会抛出异常

  

的providerName = “System.Data.SqlClient的”

答案 3 :(得分:0)

请将此添加到Nop.Web项目下的web.config:

<connectionStrings>
<add name="MyConnectionString" 
connectionString="Data Source=serverName;Initial Catalog=DBName;Persist Security Info=False;UserID=userName;Password=password" 
</connectionStrings>

最诚挚的问候。

答案 4 :(得分:0)

[1]在.NET Core(3.1 | NopCommerce 4.3)中,我创建了各种appsettings.json文件(包括appsettings.json,appsettings.Development.json,appsettings.Integration.json,appsettings.Staging.json)和逻辑(超出此讨论范围)确定在适当的环境等中使用的正确设置。

[2] \ nopCommerce \ Libraries \ Nop.Core \ Data \ DataSettingsManager.cs 我创建了以下方法:

    public static string GetConnectionString()
    {
        var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
        string appSettingsFileName = env switch
        {
            "Development" => "appsettings.Development.json",
            "Production" => "appsettings.json",
            "Staging" => "appsettings.Staging.json",
            "Integration" => "appsettings.Integration.json",
            _ => "appsettings.json",
        };

        IConfigurationRoot configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile(appSettingsFileName).Build();
        string connectionString = configuration.GetConnectionString("DefaultConnection");

        return connectionString;
    }

(为正确的环境返回正确的连接字符串)

[3]在LoadSettings中,我没有更改任何内容即可结束

Singleton<DataSettings>.Instance = JsonConvert.DeserializeObject<DataSettings>(text);

...我只是在下面添加了新行,将.ConnectionString替换为根据我们的新方法确定的连接字符串:

Singleton<DataSettings>.Instance.ConnectionString = GetConnectionString();

重要: 当我运行它时,在三个或四个地方有一个开关(case> default :)正在寻找提供者,等等-但是我只是将设置从MsSql复制到了default:并且运行良好。我知道这很草率,但我从未在该项目中使用MySql,就我所关心的这不是问题。无论哪种方式-我都将其破解以使其正常运行(多个Azure App环境更为重要)。

我建议他们应该以常规方式构建它,并只是为我们提供了一个用于部署的SQL脚本(是否设计了非问题的代码?),因为我们通常还是必须这样做以进行自定义开发(对我来说似乎很愚蠢,很难做到)在App_Data中编码数据设置文件)-但我相信它们的逻辑。