2017-07-04 129 views
1

我想将sql连接设置为config.json,并从AuditContext类中使用它。 但是,当我从PMC运行示例drop-database时,出现错误。dotnet-core ef sql connection

System.ArgumentException:初始化字符串的格式不符合规范开始于索引21. 在System.Data.Common.DbConnectionOptions.GetKeyValuePair(字符串的connectionString,的Int32 currentPosition,StringBuilder的缓冲器,字符串&键名,串&键值) 在System.Data.Common.DbConnectionOptions.ParseInternal(词典2 parsetable, String connectionString, Boolean buildChain, Dictionary 2个同义词) 在System.Data.Common.DbConnectionOptions..ctor(串的connectionString,字典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() 在Microsoft.EntityFrameworkCore。 Design.Internal.DbContextOperations.GetContextInfo(String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.GetContextInfoImpl(String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase。 <> c__DisplayClass3_0`1.b__0() 在Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(动作动作)

这里是我Startup.cs类

using System.Threading.Tasks; 
using Audit.Models.Entity; 
using Microsoft.AspNetCore.Authentication.Cookies; 
using Microsoft.AspNetCore.Builder; 
using Microsoft.AspNetCore.Hosting; 
using Microsoft.AspNetCore.Identity.EntityFrameworkCore; 
using Microsoft.Extensions.Configuration; 
using Microsoft.Extensions.DependencyInjection; 
using Microsoft.Extensions.Logging; 
using Newtonsoft.Json.Serialization; 

namespace Audit 
{ 
public class Startup 
{ 
    private IHostingEnvironment _env; 
    private IConfigurationRoot _config; 

    public Startup(IHostingEnvironment env) 
    { 
     _env = env; 

     var builder = new ConfigurationBuilder() 
      .SetBasePath(_env.ContentRootPath) 
      .AddJsonFile("config.json") 
      .AddEnvironmentVariables(); 

     _config = 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 https://go.microsoft.com/fwlink/?LinkID=398940 
    public void ConfigureServices(IServiceCollection services) 
    { 
     services.AddSingleton(_config); 

     services.AddDbContext<AuditContext>(); 

     services.AddIdentity<ApplicationUser, IdentityRole>(config => 
     { 
      config.User.RequireUniqueEmail = false; 
      config.Cookies.ApplicationCookie.LoginPath = "/auth/login"; 
      config.Cookies.ApplicationCookie.Events = new CookieAuthenticationEvents() 
      { 
       OnRedirectToLogin = async ctx => 
       { 
        if (ctx.Request.Path.StartsWithSegments("/api") && ctx.Response.StatusCode == 200) 
        { 
         ctx.Response.StatusCode = 401; 
        } 
        else 
        { 
         ctx.Response.Redirect(ctx.RedirectUri); 
        } 
        await Task.Yield(); 

       } 
      }; 
     }).AddEntityFrameworkStores<AuditContext>(); 

     services.AddLogging(); 

     services.AddMvc() 
      .AddJsonOptions(config => 
       { 
        config.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); 
       }); 

    } 

    // 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) 
    { 


     if (env.IsDevelopment()) 
     { 
      app.UseDeveloperExceptionPage(); 
      loggerFactory.AddDebug(LogLevel.Information); 
     } 
     else 
     { 
      loggerFactory.AddDebug(LogLevel.Error); 
     } 

     app.UseStaticFiles(); 

     app.UseIdentity(); 

     app.UseMvc(config => 
     { 
      config.MapRoute(
       name: "Default", 
       template: "{controller}/{action}/{id?}", 
       defaults: new { controller = "App", action = "Index" } 
      ); 
     }); 
    } 
    } 
    } 

这里是配置。 JSON

{ 
    "ConnectionStrings": { 
    "AuditDbContextConnection": "Server=xxx.xxx.xxx.xxx;Database:databasename;User Id:user;Password:password;" 
    } 
} 

这里是AuditContext.cs

​​

我做错了什么,在哪里? 谢谢。

+0

我认为在连接字符串中 '数据库', '用户ID' 后,和 '密码',你需要“= ' 并不是 ':'。我确定'='是有效的,我从来没有见过':'在那里,我想这是无效的。 – gregkalapos

+0

ROFL,我真的错过了。 100倍我看看那部分,我没有注意到... –

+0

Yeaah,我知道这一点,我想这发生在每个人身上...这就是为什么它很好地展示给其他人。 )。带'='的修补程序有帮助吗? – gregkalapos

回答

1

因此,正如评论中所讨论的那样,问题是连接字符串包含':'而不是'='。

所以不是:

{ 
    "ConnectionStrings": { 
    "AuditDbContextConnection": "Server=xxx.xxx.xxx.xxx;Database:databasename;User Id:user;Password:password;" 
    } 
} 

这将使:

{ 
    "ConnectionStrings": { 
    "AuditDbContextConnection": "Server=xxx.xxx.xxx.xxx;Database=databasename;User Id=user;Password=password;" 
    } 
}