我想将sql连接设置为config.json并从AuditContext类中使用它。 但是当我从PMC运行示例drop-database时,我收到错误。
System.ArgumentException:初始化字符串的格式不符合从索引21开始的规范。 在System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString,Int32 currentPosition,StringBuilder buffer,String& keyname,String& keyvalue) 在System.Data.Common.DbConnectionOptions.ParseInternal(Dictionary
2 parsetable, String connectionString, Boolean buildChain, Dictionary
2个同义词) 在System.Data.Common.DbConnectionOptions..ctor(String connectionString,Dictionary2 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) 在Microsoft.EntityFrameworkCore.Design.OperationExecutor.GetContextInfoImpl(String contextType) 在Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase。<> c__DisplayClass3_0`1.b__0() 在Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
这是我的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" }
);
});
}
}
}
这是config.json
{
"ConnectionStrings": {
"AuditDbContextConnection": "Server=xxx.xxx.xxx.xxx;Database:databasename;User Id:user;Password:password;"
}
}
这是AuditContext.cs
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
namespace Audit.Models.Entity
{
public class AuditContext : IdentityDbContext<ApplicationUser>
{
private IConfigurationRoot _config;
public AuditContext(IConfigurationRoot config, DbContextOptions options) : base(options)
{
_config = config;
}
public DbSet<Aql> Aql { get; set; }
public DbSet<AqlVer> AqlVer { get; set; }
public DbSet<Cartiglio> Cartiglio { get; set; }
public DbSet<CartonBox> CartonBox { get; set; }
public DbSet<CartonBoxLog> CartonBoxLog { get; set; }
public DbSet<Entry> Entry { get; set; }
public DbSet<Fashion> Fashion { get; set; }
public DbSet<Level> Level { get; set; }
public DbSet<MasterCode> MasterCode { get; set; }
public DbSet<Settings> Settings { get; set; }
public DbSet<SubCode> SubCode { get; set; }
public DbSet<TotalProduction> TotalProduction { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseSqlServer(_config["ConnectionStrings:AuditDbContextConnection"]);
}
}
}
我做错了什么? 感谢。
答案 0 :(得分:1)
因此,正如评论中所讨论的那样,问题在于连接字符串包含&#39;:&#39;而不是&#39; =&#39;。
所以而不是:
{
"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;"
}
}