0

i have a problem at creating my database in a MVC 6 ASP.NET Application. When I'm trying to add migrations in the cmd by executing "dnx ef migrations add initial", I receive the exception "The value is not allowed to be NULL. Parametername: connectionString" (it's translated from German, so in english the literal translation could be different)

I searched through the whole web, but I couldn't find anything that helped.

This is my config.json:

  {
    "AppSettings": {
      "SiteTitle": "Chronicus"
    },
    "Data": {
      "ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=Chronicus;Trusted_Connection=True;MultipleActiveResultSets=true"
    }
  }

This is my Startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Chronicus.models;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
using Microsoft.Data.Entity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.PlatformAbstractions;
using Microsoft.AspNet.Identity.EntityFramework;


namespace Chronicus
{
    public class Startup
    {

        public IConfiguration Configuration { get; set; }

        public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
        {

            // Setup configuration sources.
            var builder = new ConfigurationBuilder()
                .SetBasePath(appEnv.ApplicationBasePath)
                .AddJsonFile("config.json")
                .AddEnvironmentVariables();

            Configuration = builder.Build();
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext<ChronicusContext>(options => options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app)
        {

            /*
            // auto generated code
            app.UseIISPlatformHandler();

            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });

            */
            app.UseDefaultFiles();
            app.UseStaticFiles();
            app.UseMvc();
        }

        // Entry point for the application.
        public static void Main(string[] args) => WebApplication.Run<Startup>(args);
    }
}

This is my project.json

    {
  "version": "1.0.0-*",
  "compilationOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
    "Microsoft.AspNet.Server.WebListener": "1.0.0-rc1-final",
    "Microsoft.AspNet.Server.IIS": "1.0.0-beta5",
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
    "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
    "Microsoft.AspNet.Identity.EntityFramework": "3.0.0-rc1-final",
    "Microsoft.AspNet.Diagnostics": "1.0.0-rc1-final",
    "Microsoft.AspNet.Diagnostics.Entity": "7.0.0-rc1-final",
    "Microsoft.Framework.ConfigurationModel.Json": "1.0.0-beta4",
    "Microsoft.Framework.CodeGenerators.Mvc": "1.0.0-beta5",
    "Microsoft.Framework.Logging": "1.0.0-beta8",
    "Microsoft.Framework.Logging.Console": "1.0.0-beta8",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc1-final",
    "EntityFramework.Commands":  "7.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.FileProviderExtensions": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final",
    "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
    "EntityFramework.Core": "7.0.0-rc1-final"
  },

  "commands": {
    "web": "Microsoft.AspNet.Server.Kestrel --server.urls http://localhost:5000",
    "ef": "EntityFramework.Commands"
  },

  "frameworks": {
    "dnx451": { },
    "dnxcore50": { }
  },

  "exclude": [
    "wwwroot",
    "node_modules"
  ],
  "publishExclude": [
    "**.user",
    "**.vspscc"
  ]
}

Does anyone know, how to fix this? Thank you!

2 Answers 2

1

Unless you have a typo in your first block, this does not appear to be valid json as there is an opening { missing.

  {
    "AppSettings": {
      "SiteTitle": "Chronicus"
    },
    "Data": {
      "ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=Chronicus;Trusted_Connection=True;MultipleActiveResultSets=true"
    }
  }
}

Should be

  {
    "AppSettings": {
      "SiteTitle": "Chronicus"
    },
    "Data": {
      "ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=Chronicus;Trusted_Connection=True;MultipleActiveResultSets=true"
    }
  }

Notice the two opening {

Edit

It appears like your config.json may not be correct. Please format your config using the following example

{
    "AppSettings": {
        "SiteTitle": "WebApplication2",
    },
    "Data": {
        "DefaultConnection": {
            "ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=aspnet5-WebApplication1-414415dc-a108-49f3-a5e3-fdc4cf24ef96;Trusted_Connection=True;MultipleActiveResultSets=true"
        }
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

That was just wrong copied so it will not solve the problem, but still thank you for looking at my problem.
Now you have too many { and }. Remove the first and last ones.
@davidrue Can you verify each of the .json configuration files are valid? If you need, you can easily find JSON validators online .
Okay, seemed like it wasn't valid, i removed the first and last one and now there is the exception: "The value is not allowed to be NULL. Parametername: connectionString" (it's translated from German, so in english the literal translation could be different)
Change ConnectionString to connectionString in your config.json
|
0

Its the wrong json, you set the configuration to take the property here:

Configuration["Data:DefaultConnection:ConnectionString"])

Your json need to be this:

{   
"AppSettings": {
      "SiteTitle": "Chronicus"   
},   
"Data": {
        "DefaultConnection": {
          "ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=Chronicus;Trusted_Connection=True;MultipleActiveResultSets=true"
                             }   
        } 
}

Other problem can be the method to take the config, Try this:

Configuration.Get("Data:DefaultConnection:ConnectionString");

instead of this:

Configuration["Data:DefaultConnection:ConnectionString"];

For more informations, try this link.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.