Lor*_*fur 2 c# entity-framework-core asp.net-core
我试图从appSettings.json文件中获取连接字符串,但我不能。
我的启动是这样的:
namespace YangSoft_WebAPI
{
public class Startup
{
public IConfiguration Configuration { get; }
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();
Configuration = builder.Build();
var enviroment = Configuration["ApplicationSettings:Enviroment"];
}
readonly string AllowControlOrigins = "Access-Control-Allow-Origin";
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<AppSettingsDAL>(Configuration.GetSection("ApplicationSettings"));
services.AddDbContextPool<yangsoftDBContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("local")));
services.AddCors(options =>
{
options.AddDefaultPolicy(
builder =>
{
builder.WithOrigins("*");
});
options.AddPolicy(AllowControlOrigins,
builder =>
{
builder.WithOrigins("http://localhost:3000",
"https://localhost:3000",
"http://yangsoft-frontend.s3-website.us-east-2.amazonaws.com")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddSwaggerGen(c => {
c.SwaggerDoc("v1", new Info
{
Version = "v1",
Title = "Test API",
Description = "ASP.NET Core Web API"
});
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseCors(AllowControlOrigins);
app.UseHttpsRedirection();
app.UseMvc();
app.UseSwagger();
app.UseSwaggerUI(c => {
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Test API V1");
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
在我的启动中你怎么能看到,我初始化了 DBContext,在 mi DBLogic 之后我有这个:
public partial class yangsoftDBContext : DbContext
{
private readonly AppSettingsDAL _appSettings;
public yangsoftDBContext(DbContextOptions<yangsoftDBContext> options)
: base(options)
{
}
public virtual DbSet<Acceso> Acceso { get; set; }
public virtual DbSet<Actividad> Actividad { get; set; }
public virtual DbSet<Auditoria> Auditoria { get; set; }
......................
}
Run Code Online (Sandbox Code Playgroud)
这是我在 webLogic 的合作伙伴课程中的示例方法到 DAL:
public int CountActiveSocios()
{
using (var context = new yangsoftDBContext())
{
try
{
return context.Socios.Where(r => r.Estado == true).Count();
}
catch
{
return 0;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在所有我称之为youngsoft DBContext .net 的地方都会返回这个错误:
“没有参数给出对应于所需的形式参数'选项'”
您可以执行以下操作:
public int CountActiveSocios()
{
IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();
var connectionString = configuration.GetConnectionString("local");
var options = new DbContextOptionsBuilder<yangsoftDBContext>()
.UseSqlServer(new SqlConnection(connectionString))
.Options;
using (var context = new yangsoftDBContext(options)) // <-- Pass the options here
{
try
{
return context.Socios.Where(r => r.Estado == true).Count();
}
catch
{
return 0;
}
}
}
Run Code Online (Sandbox Code Playgroud)
要重用DbContextOptions你可以编写一个辅助方法,如下所示:
public static class DbContextHelper
{
public static DbContextOptions<yangsoftDBContext> GetDbContextOptions()
{
IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();
return new DbContextOptionsBuilder<yangsoftDBContext>()
.UseSqlServer(new SqlConnection(configuration.GetConnectionString("local"))).Options;
}
}
Run Code Online (Sandbox Code Playgroud)
然后使用如下:
public int CountActiveSocios()
{
var dbContextOptions = DbContextHelper.GetDbContextOptions();
using (var context = new yangsoftDBContext(dbContextOptions)) // <-- Pass the options here
{
try
{
return context.Socios.Where(r => r.Estado == true).Count();
}
catch
{
return 0;
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3293 次 |
| 最近记录: |