构造函数与app.config中的连接字符串?

时间:2017-11-16 17:33:18

标签: c# .net asp.net-web-api2 visual-studio-2017 azure-service-fabric

在我的上下文构造函数中引用配置值时,我没有遇到数据库连接问题:

public class MyDbContext : DbContext
{
    public MyDbContext ()
        : base("Name=Model1")
    {
    }
}

我的App.config连接字符串如下所示:

<connectionStrings>
    <add name="Model1"
         connectionString="data source=10.4.4.3;initial catalog=mydatabase;persist security info=True;user id=myuser;password=mypass;MultipleActiveResultSets=True;App=EntityFramework"
         providerName="System.Data.SqlClient" />
  </connectionStrings>

然而,当我像这样对连接进行硬编码时:

public class MyDbContext : DbContext
{
    public MyDbContext ()
        : base(@"data source=10.4.4.3;initial catalog=mydatabase;persist security info=True;user id=myuser;password=mypass;MultipleActiveResultSets=True;App=EntityFramework")
    {
    }
}

我得到的例外是:

{System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.Configuration.ConfigurationErrorsException: Unrecognized element. (C:\ProgramData\SF\vm0\Fabric\work\Applications\abc.API.ProdAndPrice.O720.ProdLinesType_App285\ComponentsPkg.Code.1.0.0\Components.exe.Config line 38)
   at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
   at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)
   at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
   --- End of inner exception stack trace ---
   at System.Configuration.ConfigurationManager.PrepareConfigSystem()
   at System.Configuration.ConfigurationManager.GetSection(String sectionName)
   at System.Configuration.PrivilegedConfigurationManager.GetSection(String sectionName)
   at System.Diagnostics.DiagnosticsConfiguration.GetConfigSection()
   at System.Diagnostics.DiagnosticsConfiguration.Initialize()
   at System.Diagnostics.DiagnosticsConfiguration.get_IndentSize()
   at System.Diagnostics.TraceInternal.InitializeSettings()
   at System.Diagnostics.TraceInternal.get_Listeners()
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
   at Microsoft.Owin.Hosting.Utilities.SettingsLoader.FromConfigImplementation..ctor()
   at Microsoft.Owin.Hosting.Utilities.SettingsLoader.<LoadFromConfig>b__0()
   at System.Threading.LazyInitializer.EnsureInitializedCore[T](T& target, Func`1 valueFactory)
   at Microsoft.Owin.Hosting.Utilities.SettingsLoader.LoadFromConfig(IDictionary`2 settings)
   at Microsoft.Owin.Hosting.Engine.StartContext..ctor(StartOptions options)
   at Microsoft.Owin.Hosting.WebApp.StartImplementation(IServiceProvider services, StartOptions options, Action`1 startup)
   at Microsoft.Owin.Hosting.WebApp.Start(String url, Action`1 startup)
   at Components.OwinCommunicationListener.OpenAsync(CancellationToken cancellationToken) in F:\TFS\ooo720 Res\ooo720\DEV\Source\enterprise\abc.API.ProdAndPrice.O720.ProdLines\Components\OwinCommunicationListener.cs:line 78
   at Microsoft.ServiceFabric.Services.Runtime.StatelessServiceInstanceAdapter.<OpenCommunicationListenersAsync>d__19.MoveNext()}

我做错了什么?为什么连接失败?

1 个答案:

答案 0 :(得分:1)

在为其分配密钥时,不需要app.config中的连接字符串,也不需要使用它,只需在构造函数中传递连接字符串