我在使用EF Core bsaed应用程序将ASP.NET添加到ASP.NET Core MVC时遇到问题,其中抛出了ArgumentNullException,异常的Message属性读作"参数名称: connectionString StackTrace:参数名称:connectionString"。
我的应用程序是关注Tom Dykstra和Rick Anderson在https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro
上的docs.microsoft.com上的Contoso大学教程当我尝试通过选择"带视图的MVC控制器来添加脚手架控制器时,使用实体框架"使用“添加控制器”对话框,通过“添加脚手架”对话框,使用以下设置:
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
单击“添加控制器”对话框中的“添加”按钮,将为构建过程添加以下输出,以便为控制器添加脚手架:
C:\ Program Files \ dotnet \ dotnet.exe aspnet-codegenerator --project " C:\工作\ NET \ ContosoUniversity \ ContosoUniversity \ ContosoUniversity.csproj" --no-build controller --force --controllerName StudentsController --model ContosoUniversity.Models.Student --dataContext ContosoUniversity.Data.SchoolContext --relativeFolderPath Controllers --referenceScriptLibraries --useDefaultLayout命令行: - 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版本15.1.548.43366版权所有(C)Microsoft Corporation。所有 保留的权利。命令行: - 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查找生成器' controller' ...运行生成器' controller' ...尝试编译申请 内存尝试找出的EntityFramework元数据 model和DbContext:Student Value不能为null.Value不能 空。
参数名称:connectionString StackTrace:参数名称: 的connectionString
at Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(String value,String parameterName)at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.b__6_0()
在 Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseSqlServer(DbContextOptionsBuilder optionsBuilder,String connectionString,Action`1 sqlServerOptionsAction)at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(字符串[] 参数)
在 ContosoUniversity.Startup.b__4_0(DbContextOptionsBuilder 选项)at Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(字符串[] 参数)
在 Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.DbContextOptionsFactory [TContext](的IServiceProvider applicationServiceProvider,Action`2 optionsAction)at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite,ServiceProvider提供者)at Microsoft.Extensions.DependencyInjection.ServiceProvider<> c__DisplayClass16_0.b__0(的ServiceProvider 提供者) Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(的IServiceProvider provider,输入serviceType)at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService [T](的IServiceProvider 提供者) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite,ServiceProvider提供者)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 提供者) Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(的IServiceProvider provider,输入serviceType)at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService [T](的IServiceProvider 提供者) Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.FindContextTypes() 在 Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.FindContextType(字符串 姓名)at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(字符串 contextType)at Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore.EntityFrameworkServices.TryCreateContextUsingAppCode(类型 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数据库,但未定义任何表,并且仍然接收相同的当我尝试创建Controller时,以及通过IISExpress运行MVC Web应用程序时ArgumentNullException:
"ConnctionStrings": {
"DefaultConnection": "Server=Skittles;Database=ContosoUniversity1;Trusted_Connection=True;MultipleActiveResultSets=true"
},
我正在使用Visual Studio Community 2017的实际版本,并为ASP.NET Core和Entity Framework Core提供以下NuGet包引用 (取自.Resp文件&#39;用于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" />
我将非常感谢您就如何解决此问题提出任何建议或想法。
提前感谢您的时间,帮助和耐心。
答案 0 :(得分:2)
调试启动并确保Configuration.GetConnectionString("DefaultConnection")
实际返回一个值。那是你应该先检查的地方。
您会注意到null
,因为在设置文件中拼错了密钥ConnctionStrings
更改
"ConnctionStrings": {...
},
到
"ConnectionStrings": {...
},