2017-04-05 50 views
2

我想我的ASP网核心应用程序与Microsoft SQL Server管理,但在我的第一个迁移创建我得到这个错误我的数据库连接:ASP网核心:连接到现有的SqlServer

System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0. 
    at System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, String& keyname, String& keyvalue) 
    at System.Data.Common.DbConnectionOptions.ParseInternal(Dictionary`2 parsetable, String connectionString, Boolean buildChain, Dictionary`2 synonyms) 
    at System.Data.Common.DbConnectionOptions..ctor(String connectionString, Dictionary`2 synonyms) 
    at System.Data.SqlClient.SqlConnectionString..ctor(String connectionString) 
    at System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous) 
    at System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(DbConnectionPoolKey key, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions) 
    at System.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey key) 
    at System.Data.SqlClient.SqlConnection.set_ConnectionString(String value) 
    at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerConnection.CreateDbConnection() 
    at Microsoft.EntityFrameworkCore.Internal.LazyRef`1.get_Value() 
    at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration) 
    at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType) 
    at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_1.<.ctor>b__0() 
    at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) 
Format of the initialization string does not conform to specification starting at index 0. 

我不不知道我做错了什么,所以如果有人能帮助我。

这里是我的连接字符串:

"Data": { 
    "DefaultConnection": { 
     "ConnectionString": "Data Source=..\\SQLSERVER;Initial Catalog=dbfichetips;Integrated Security=True;" 
    } 

而且我Startup.cs

public class Startup 
{ 
    public Startup(IHostingEnvironment env) 
    { 
     var builder = new ConfigurationBuilder() 
      .SetBasePath(env.ContentRootPath) 
      .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) 
      .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) 
      .AddEnvironmentVariables().Build(); 
    } 


    // This method gets called by the runtime. Use this method to add services to the container. 
    public void ConfigureServices(IServiceCollection services) 
    { 
     services.AddMvc(); 

     services.AddDbContext<ApplicationDbContext>(
      options => options.UseSqlServer("Data:DefaultConnection:ConnectionString")); 

     services.AddIdentity<ApplicationUser, ApplicationRole>() 
      .AddEntityFrameworkStores<ApplicationDbContext, int>() 
      .AddDefaultTokenProviders(); 

     services.Configure<IdentityOptions>(options => 
     { 
      //Password settings 
      options.Password.RequireDigit = false; 
      options.Password.RequiredLength = 6; 
      options.Password.RequireLowercase = false; 
      options.Password.RequireNonAlphanumeric = false; 
      options.Password.RequireUppercase = false; 
     }); 

    } 

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, ApplicationDbContext applicationDbContext) 
    { 
     loggerFactory.AddConsole(); 
     loggerFactory.AddDebug(); 

     if (env.IsDevelopment()) 
     { 
      app.UseDeveloperExceptionPage(); 
      app.UseBrowserLink(); 
      applicationDbContext.Database.Migrate(); 
     } 
     else 
     { 
      app.UseExceptionHandler("/Home/Error"); 
     } 

     app.UseIdentity(); 
     app.UseStaticFiles(); 

     app.UseMvc(routes => 
     { 
      routes.MapRoute(
       name: "default", 
       template: "{controller=Home}/{action=Index}/{id?}"); 
     }); 
    } 
} 
+0

我不知道,如果'= .. \\'在数据源属性是有效 – Smartis

+0

这是当我看到我的服务器的性能在服务器资源管理器 编辑我得到什么:我得到这个数据源= KA- 003 \\ SQLSERVER;但错误仍然在这里 – Elykx

+0

尝试'= {在此处插入SQL Server IP地址} \ SQLSERVER;'作为数据源属性 – Smartis

回答

6

UseSqlServer预计 “真实” 的连接字符串,而不是在配置键名。

而不是

services.AddDbContext<ApplicationDbContext>(
     options => options.UseSqlServer("Data:DefaultConnection:ConnectionString")); 

你需要

services.AddDbContext<ApplicationDbContext>(
     options => options.UseSqlServer(builder["Data:DefaultConnection:ConnectionString"])); 
变量/属性

并保存builder - 目前它的Startup构造完成后,垃圾收集。

+0

感谢它现在的作品 – Elykx