none
net core2.0連線 RRS feed

  • 問題

  • 各位好:

          想請問一下 我在學net core 但是卻遇到無法連線,
    程式碼完全跟微軟 教學一樣 https://docs.microsoft.com/zh-tw/aspnet/core/data/ef-mvc/intro?view=aspnetcore-2.0

    ,實在找不出原因,錯誤訊息是 Value cannot be null. Parameter name: connectionString ,以下是我的相關程式碼
    感謝!!

    1.appsettings.json

    {
      "Logging": {
        "IncludeScopes": false,
        "LogLevel": {
          "Default": "Warning"
        },
        "ConnectionStrings": {
          "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=ContosoUniversity1;Trusted_Connection=True;MultipleActiveResultSets=true"
        },
        "MyConfig": "My Config Value for staging."
      }
    }

    2.Startup.cs

       public class Startup
        {
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }
    
            public IConfiguration Configuration { get; }
    
            public void ConfigureServices(IServiceCollection services)
            {
                var connStr = Configuration.GetConnectionString("DefaultConnection");
                services.AddDbContext<SchoolContext>(options =>
                    options.UseSqlServer(connStr));
                services.AddMvc();
            }
    }

    2018年5月22日 下午 10:31

解答

所有回覆