Проблема, с которой я столкнулась, - это когда я запускаю программу, я получаю исключение «Исключение System.NullReferenceException». Не знаю, в чем проблема. Эта проблема также не позволяет мне начать миграцию данных с помощью dnx.ASP.NET MVC 6 Connection Sting config.json
#config.json
{
"Data": {
"DefaultConnection": {
"Connectionstring": "Data Server=.\\ERKANDEMIR5E91;Initial Catalog=RegistrationDB;Integrated Security=True;"
}
}
}
scoolDbContext.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Metadata;
using School.Models;
namespace School.Context
{
public class SchoolDbContext : DbContext
{
public DbSet<Course> Courses {get; set;}
}
}
Statup.cs
namespace School
{
public class Startup
{
public static Microsoft.Extensions.Configuration.IConfiguration Configuration { get; set; }
public Startup (IHostingEnvironment env)
{
//setup configuration sources
Configuration = new ConfigurationBuilder()
.AddJsonFile("config.json")
.AddEnvironmentVariables()
.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)
{
//add services to container
services.AddEntityFramework().AddSqlServer().AddDbContext<SchoolDbContext>(options => options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
services.AddMvc();
//resolve dependency injections
services.AddScoped<IRegistrationRepo, RegistrationRepo>();
services.AddScoped<SchoolDbContext, SchoolDbContext>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app)
{
app.UseIISPlatformHandler();
app.UseMvc();
app.UseWelcomePage();
}
// Entry point for the application.
public static void Main(string[] args) => WebApplication.Run<Startup>(args);
}
}
Не могли бы вы добавить еще полный код 'Statup.cs' (используя, например). Не удалось удалить «services.AddScoped();'? Я лично предпочел бы удалить «OnConfiguring» из «SchoolDbContext» и использовать «.AddDbContext (options => options.UseSqlServer (Configuration [" Data: DefaultConnection: ConnectionString "]));'. Вы можете попробовать переименовать 'config.json' в' appsettings.json', который теперь является стандартным именем app-config. –
Oleg
попытайтесь переименовать 'Data Source =. \\ ERKANDEMIR5E91' в' Server =. \\ ERKANDEMIR5E91; 'в' config.json'. Синтаксис будет означать, что вы используете SQL srever на текущем компьютере, а 'ERKANDEMIR5E91' - это имя экземпляра SQL Server. Это то, что вы делаете? – Oleg
Эй, @ Олег, я все еще получаю ту же ошибку. FYI, 'ERKANDEMIR5E91' - это имя SQL-сервера. –