2016-11-26 2 views
1

Asp.net 핵심 응용 프로그램을 만들고 데이터베이스에 연결하려고합니다. startup.cs 파일의 ConfigureServices 메서드에서 다음 코드 줄에 오류가 발생했습니다. 내가 얻는 오류는 값이 null 일 수 없다는 것입니다. web.config 파일에서 CustomerOrderEntities 키를 찾을 수없는 것 같습니다. 문제가 Startup.csAsp.Net 핵심 dbcontext 문제

services.AddDbContext<CustomerOrderEntities>(options => 
        options.UseSqlServer(Configuration.GetConnectionString("CustomerOrderEntities"))); 

무엇인지 확실하지

public IServiceProvider ConfigureServices(IServiceCollection services) 
     { 
services.AddDbContext<CustomerOrderEntities>(options => 
       options.UseSqlServer(Configuration.GetConnectionString("CustomerOrderEntities"))); 

AutoMapperConfiguration.Configure(); 

      // Create the container builder. 
      var builder = new ContainerBuilder(); 

      // Register dependencies, populate the services from 
      // the collection, and build the container. If you want 
      // to dispose of the container at the end of the app, 
      // be sure to keep a reference to it as a property or field. 


      builder.RegisterType<UnitOfWork>().As<IUnitOfWork>(); 
      builder.RegisterType<DbFactory>().As<IDbFactory>(); 
      builder.RegisterType<CustomerRepository>().As<ICustomerRepository>(); 
      builder.RegisterType<ProductRepository>().As<IProductRepository>(); 
      builder.RegisterType<OrderRepository>().As<IOrderRepository>(); 



      builder.Populate(services); 
      this.ApplicationContainer = builder.Build(); 

      // Create the IServiceProvider based on the container. 
      return new AutofacServiceProvider(this.ApplicationContainer); 
      } 

ASP.NET 코어에 연결 문자열이 appsettings.json에 정의되어

<connectionStrings> 

    <add name="CustomerOrderEntities" connectionString="metadata=res://*/EF.CustomerOrderContext.csdl|res://*/EF.CustomerOrderContext.ssdl|res://*/EF.CustomerOrderContext.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=test-PC\MSSQLSERVER2014;initial catalog=CodeFirstTest;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> 

    </connectionStrings> 
+0

EntityFramework 6를 사용하고 있습니까? 예를 들어 EFCore와 EF 코어는 web.config를 사용하지 않지만 asp.net 코어에있는 appsettings.json (및 로컬 개발에 대한 사용자 비밀)은 – Tseng

+0

입니다. 우리는 web.config에 그런 종류의 물건을 저장하지 않습니다. web.config는 IIS 구성 전용이며 appsettings.json –

+0

을 사용해야합니다. 죄송합니다. 그것은 entityframeworkcore 아니 6 – Tom

답변

1

의 Web.config 예 :

"ConnectionStrings": { 
    "CustomerOrderEntities": "Server=(localdb)\\mssqllocaldb;Database=aspnet-a3e892a5-6a9c-4090-bc79-fe8c79e1eb26;Trusted_Connection=True;MultipleActiveResultSets=true" 
    }, 

연결 문자열의 사례 이름이 CustomerOrderEntities 인 경우 null이 표시됩니다. 아마도 거기에 없기 때문에 appsettings.json을 확인하십시오.

0

Appsettings.json의 연결 고리가 분명히 없습니다. 전체 .net 프레임 워크 또는 .net 코어를 대상으로하는지 여부에 관계없이 Asp.net 코어의 web.config에서 연결 문자열을 설정할 수 없습니다. 또는 서비스에 연결 문자열 값을 입력합니다 .AddDbContext (options => options.UseSqlServer ("Your Connectionstring")); 자세한 내용은 여기를 확인하십시오.

https://docs.microsoft.com/en-us/ef/core/