我在使用 EF Core bsaed 应用程序将 Controller 添加到 ASP.NET Core MVC 时遇到问题,其中抛出 ArgumentNullException,异常的 Message 属性读取为“参数名称:connectionString StackTrace:参数名称:connectionString”。
我的申请遵循 Tom Dykstra 和 Rick Anderson 在 learn.microsoft.com 上发布的 Contoso 大学教程,网址为 https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro
当我尝试通过使用具有以下设置的“添加 Controller ”对话框为 Student 类的“添加脚手架”对话框选择“带有 View 的 MVC Controller ,使用 Entity Framework ”来添加脚手架 Controller 时:
Model class: Student (ContosoUniversity.Models)
Data context class: SchoolContext (ContosoUniversity.Data)
Views: [All of the following are checked]
Generate views
Reference script libraries
Use a layout page
(The layout textbox is left empty as it is set in a Razor _viewstart file)
Controller name: StudentController
单击“添加 Controller ”对话框中的“添加”按钮会生成以下用于添加 Controller 脚手架的构建过程的输出:
C:\Program Files\dotnet\dotnet.exe aspnet-codegenerator --project "C:\work\NET\ContosoUniversity\ContosoUniversity\ContosoUniversity.csproj" --no-build controller --force --controllerName StudentsController --model ContosoUniversity.Models.Student --dataContext ContosoUniversity.Data.SchoolContext --relativeFolderPath Controllers --referenceScriptLibraries --useDefaultLayout Command Line: --project C:\work\NET\ContosoUniversity\ContosoUniversity\ContosoUniversity.csproj --no-build controller --force --controllerName StudentsController --model ContosoUniversity.Models.Student --dataContext ContosoUniversity.Data.SchoolContext --relativeFolderPath Controllers --referenceScriptLibraries --useDefaultLayout Microsoft (R) Build Engine version 15.1.548.43366 Copyright (C) Microsoft Corporation. All rights reserved. Command Line: --no-dispatch --port-number 5716 --project C:\work\NET\ContosoUniversity\ContosoUniversity\ContosoUniversity.csproj --no-build controller --force --controllerName StudentsController --model ContosoUniversity.Models.Student --dataContext ContosoUniversity.Data.SchoolContext --relativeFolderPath Controllers --referenceScriptLibraries --useDefaultLayout --dispatcher-version 1.0.0-rtm-10308 Finding the generator 'controller'... Running the generator 'controller'... Attempting to compile the application in memory Attempting to figure out the EntityFramework metadata for the model and DbContext: Student Value cannot be null.Value cannot be null.
Parameter name: connectionString StackTrace:Parameter name: connectionString
at Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(String value, String parameterName) at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.b__6_0()
at Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseSqlServer(DbContextOptionsBuilder optionsBuilder, String connectionString, Action`1 sqlServerOptionsAction) at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
at ContosoUniversity.Startup.b__4_0(DbContextOptionsBuilder options) at Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] args)
at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.DbContextOptionsFactory[TContext](IServiceProvider applicationServiceProvider, Action`2 optionsAction) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.b__0(ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitClosedIEnumerable(ClosedIEnumerableCallSite closedIEnumerableCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.b__0(ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.FindContextTypes() at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.FindContextType(String name) at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType) at Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore.EntityFrameworkServices.TryCreateContextUsingAppCode(Type dbContextType, Type startupType) RunTime 00:00:17.43
我在 appsettings.json 中的连接字符串定义如下(使用 localdb):
"ConnctionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=ContosoUniversity1;Trusted_Connection=True;MultipleActiveResultSets=true"
},
Startup 类中的ConfigureServices()方法的实现如下:
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<SchoolContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddMvc();
}
作为使用 SQL Server Express LocalDB 的替代方法,我还尝试使用类似于以下内容的连接字符串设置,该设置连接到名称为 ContosoUniversity1 的现有 SQL Server 数据库(未定义任何表),并且在以下情况下仍然收到相同的 ArgumentNullException:我尝试创建 Controller ,并通过 IISExpress 运行 MVC Web 应用程序时:
"ConnctionStrings": {
"DefaultConnection": "Server=Skittles;Database=ContosoUniversity1;Trusted_Connection=True;MultipleActiveResultSets=true"
},
我使用的是 Visual Studio Community 2017 的实际版本,并为 ASP.NET Core 和 Entity Framework Core 使用了以下 NuGet 包引用 (取自 .csproj 文件的 PackageReferences:
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.1"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.1.0" />
任何有关如何解决此问题的建议或想法将不胜感激。
预先感谢您的时间、帮助和耐心。
最佳答案
调试启动并确保 Configuration.GetConnectionString("DefaultConnection")
实际上返回一个值。这是您应该首先检查的地方。
您会注意到它是null
,因为在设置文件中您将 key 拼写错误ConnctionStrings
改变
"ConnctionStrings": {...
},
至
"ConnectionStrings": {...
},
关于asp.net-mvc - Entity Framework Core 1.1.1 连接字符串引发 ArgumentNullException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43037189/