ASP.NET Core和EF应用程序停止初始化数据库上下文

时间:2019-03-31 01:29:16

标签: c# .net entity-framework asp.net-core

我有.NET C#解决方案,

  1. ASP.NET Core项目参考
  2. 包含EF的控制台应用程序。

EF是数据库优先模式。

每当我运行应用程序调试时,我都可以跟踪

// --------------------------------------------- --------------------------------- // //此代码是从模板生成的。 // //对此文件进行手动更改可能会导致应用程序发生意外行为。 //如果重新生成代码,则对该文件的手动更改将被覆盖。 // // ------------------------------------------------ ------------------------------

namespace ProductApi.Database
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class ProductsEntities1 : DbContext
    {
        public ProductsEntities1()
            : base("name=ProductsEntities1")
        {
        }

:base(“ name = ProductsEntities1”) 然后它停在那里并引发错误:

  

2019-03-31   12:21:20.6795 | 1 | ERROR | Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware | An   执行请求时发生未处理的异常。   Newtonsoft.Json.JsonSerializationException:从中获取值时出错   “结果”于   'System.Threading.Tasks.Task`1 [ProductApi.Responses.ProductsResponse]'。

     

---> System.AggregateException:发生一个或多个错误。 (“ System.Data.Entity.Internal.AppConfig”的类型初始值设定项   例外。)   ---> System.TypeInitializationException:'System.Data.Entity.Internal.AppConfig'的类型初始值设定项引发了异常。   ---> System.IO.FileNotFoundException:无法加载文件或程序集'System.Configuration.ConfigurationManager,版本= 0.0.0.0,   文化=中性,PublicKeyToken = cc7b13ffcd2ddd51'。系统无法   查找指定的文件。在   System.Data.Entity.Internal.AppConfig..ctor()位于   System.Data.Entity.Internal.AppConfig..cctor()-内部结束   异常堆栈跟踪---   System.Data.Entity.Internal.LazyInternalConnection..ctor(DbContext   上下文,字符串nameOrConnectionString)在   System.Data.Entity.DbContext..ctor(字符串名称或连接字符串)
  在位于以下位置的ProductApi.Database.ProductsEntities1..ctor()   E:\ work \ ProductApi \ ProductApi.Database \ ProductsModel.Context.cs:line   19在ProductApi.Database.ProductRepository..ctor()中   E:\ work \ ProductApi \ ProductApi.Database \ ProductRepository.cs:第14行
  在ProductApi.Queries.Handlers.ProductsHandler.Handle(ProductsQuery   请求,CancelationToken cancelToken)在   E:\ work \ ProductApi \ ProductApi \ Queries \ Handlers \ ProductsHandler.cs:line   16点   MediatR.Pipeline.RequestPostProcessorBehavior 2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate 1   下一个)   MediatR.Pipeline.RequestPreProcessorBehavior 2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate 1   下一个)-内部异常堆栈跟踪的结尾-在   System.Threading.Tasks.Task 1.GetResultCore(Boolean waitCompletionNotification) at Newtonsoft.Json.Serialization.ExpressionValueProvider.GetValue(Object target) --- End of inner exception stack trace --- at Newtonsoft.Json.Serialization.ExpressionValueProvider.GetValue(Object target) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType) at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType) at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value) at Microsoft.AspNetCore.Mvc.Formatters.JsonOutputFormatter.WriteObject(TextWriter writer, Object value) at Microsoft.AspNetCore.Mvc.Formatters.JsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultAsync(IActionResult result) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResultFilterAsync[TFilter,TFilterAsync]() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResultExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultFilters() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context) at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext) at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context) 2019-03-31 12:21:20.7090|2|WARN|Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware|The response has already started, the error page middleware will not be executed. 2019-03-31 12:21:20.7372|13|ERROR|Microsoft.AspNetCore.Server.Kestrel|Connection id "0HLLLES9AKJ2S", Request id "0HLLLES9AKJ2S:00000002": An unhandled exception was thrown by the application. Newtonsoft.Json.JsonSerializationException: Error getting value from 'Result' on 'System.Threading.Tasks.Task
1 [ProductApi.Responses.ProductsResponse]”。   ---> System.AggregateException:发生一个或多个错误。 (“ System.Data.Entity.Internal.AppConfig”的类型初始值设定项   例外)---> System.TypeInitializationException:类型   “ System.Data.Entity.Internal.AppConfig”的初始值设定项引发了   例外。 ---> System.IO.FileNotFoundException:无法加载文件   或程序集'System.Configuration.ConfigurationManager,   版本= 0.0.0.0,文化=中性,PublicKeyToken = cc7b13ffcd2ddd51'。   该系统找不到指定的文件。在   System.Data.Entity.Internal.AppConfig..ctor()位于   System.Data.Entity.Internal.AppConfig..cctor()-内部结束   异常堆栈跟踪---   System.Data.Entity.Internal.LazyInternalConnection..ctor(DbContext   上下文,字符串nameOrConnectionString)在   System.Data.Entity.DbContext..ctor(字符串名称或连接字符串)
  在位于以下位置的ProductApi.Database.ProductsEntities1..ctor()   E:\ work \ ProductApi \ ProductApi.Database \ ProductsModel.Context.cs:line   19在ProductApi.Database.ProductRepository..ctor()中   E:\ work \ ProductApi \ ProductApi.Database \ ProductRepository.cs:第14行
  在ProductApi.Queries.Handlers.ProductsHandler.Handle(ProductsQuery   请求,CancelationToken cancelToken)在   E:\ work \ ProductApi \ ProductApi \ Queries \ Handlers \ ProductsHandler.cs:line   16点   MediatR.Pipeline.RequestPostProcessorBehavior 2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate 1   下一个)   MediatR.Pipeline.RequestPreProcessorBehavior 2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate 1   下一个)-内部异常堆栈跟踪的结尾-在   System.Threading.Tasks.Task 1.GetResultCore(Boolean waitCompletionNotification) at Newtonsoft.Json.Serialization.ExpressionValueProvider.GetValue(Object target) --- End of inner exception stack trace --- at Newtonsoft.Json.Serialization.ExpressionValueProvider.GetValue(Object target) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType) at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType) at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value) at Microsoft.AspNetCore.Mvc.Formatters.JsonOutputFormatter.WriteObject(TextWriter writer, Object value) at Microsoft.AspNetCore.Mvc.Formatters.JsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultAsync(IActionResult result) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResultFilterAsync[TFilter,TFilterAsync]() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResultExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultFilters() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context) at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext) at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Server.IISIntegration.IISMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication
1   应用程序)

这是我的数据库的connectionString:

<add name="ProductsEntities1" connectionString="metadata=res://*/ProductsModel.csdl|res://*/ProductsModel.ssdl|res://*/ProductsModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=DESKTOP-IAJ5HKV\SQLEXPRESS;initial catalog=Products;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

此错误信息是如此通用,不要知道该从哪里开始弄清楚。请帮忙。

1 个答案:

答案 0 :(得分:0)

尝试像这样读取您的连接字符串,而不是使用ConfigurationManager:

var connectionString = Configuration["connectionStrings:ProductsEntities1"];

我不确定该连接字符串是否有效。您可以检查https://www.connectionstrings.com/sql-server/了解更多信息。