- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
免责声明:本人是菜鸟,正在努力学习net 6.0。
这是我正在尝试做的:
这是我所做的:
大多数上下文文件都是自动生成的,我唯一做的就是更新 public TransparencyContext(IConfiguration config)
以包含 Iconfig,这样我就可以在 optionsBuilder.UseSqlServer( config.GetConnectionString("SQLDB"));
透明上下文.cs
namespace EFTutorial.Models
{
public partial class TransparencyContext : DbContext
{
private readonly IConfiguration config;
public TransparencyContext()
{
}
public TransparencyContext(IConfiguration config)
{
this.config = config;
}
public TransparencyContext(DbContextOptions<TransparencyContext> options)
: base(options)
{
}
public virtual DbSet<Fundraiser> Fundraisers { get; set; } = null!;
public virtual DbSet<Person> Persons { get; set; } = null!;
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(config.GetConnectionString("SQLDB"));
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Fundraiser>(entity =>
{
entity.Property(e => e.Description).IsUnicode(false);
entity.Property(e => e.EndDate).HasColumnType("datetime");
entity.Property(e => e.Goal).HasColumnType("decimal(18, 2)");
entity.Property(e => e.Name)
.HasMaxLength(1000)
.IsUnicode(false);
});
modelBuilder.Entity<Person>(entity =>
{
entity.Property(e => e.DateOfBirth).HasColumnType("datetime");
entity.Property(e => e.FirstName)
.HasMaxLength(100)
.IsUnicode(false);
entity.Property(e => e.LastName)
.HasMaxLength(100)
.IsUnicode(false);
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
}
然后我尝试通过这样做从家庭 Controller 对其进行测试。
private TransparencyContext _transparencyContext;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
_transparencyContext = new();
}
public IActionResult Index()
{
var p = new Person();
p.FirstName = "Entity";
p.LastName = "Framework";
_transparencyContext.Persons.Add(p);
_transparencyContext.SaveChanges();
return View();
}
当我这样做时,我得到配置变量(在 TransparencyContext 中)为空(System.ArgumentNullException: 'Value cannot be null(Parameter 'connectionString')')。我没有更改我的 program.cs,这是创建项目时的方式。
程序.cs
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// 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.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
我知道可以从 app.Configuration
访问配置文件,但不确定如何使 TransparencyContext.cs 可以访问配置,以便我可以获得 db 的连接字符串。我已经尝试查看Microsoft 文档,但他们没有展示他们如何使 Iconfiguration 可用,只展示了他们如何使用它。非常感谢任何帮助。
我在想我可能需要将服务注册到配置,但不确定如何操作。
应用设置.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"SQLDB": "Server=AzureConnectionStringCopyPaste"
}
}
最佳答案
快速修复当前设置的方法是注入(inject) IConfiguration
进入 Controller 并使用它来构建上下文:
public HomeController(ILogger<HomeController> logger, IConfiguration cfg)
{
_logger = logger;
_transparencyContext = new(cfg);
}
但是“正确”和惯用的方法是使用 DI 到 register and resolve上下文:
TransparencyContext(DbContextOptions<TransparencyContext> options)
之外的所有构造函数AddDbContext
在 DI 中注册上下文或 AddDbContextFactory
:builder.Services.AddDbContextFactory<TransparencyContext>(opts =>
opts.UseSqlServer(builder.Configuration.GetConnectionString("SQLDB")));
public HomeController(ILogger<HomeController> logger, TransparencyContext ctx)
{
_logger = logger;
_transparencyContext = ctx;
}
关于c# - NetCore 6.0 如何访问dbContext文件中的连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70628152/
在一个新的 ASP.NET Core RC2 类库中,我有以下 project.json 文件,我试图在其中遵循 How to trim your package dependencies 上的文档.
我在本地提要上将 nuget 包从 2.2.x 更新到 3.1.0。 然后,在构建机器上尝试构建项目,但是: Project.csproj(0,0): Error NU1102: Unable to
快递100的物流信息查询接口,官方提供了一些demo;还好官方提供的代码是.netcore版本写的,不过写的有点low;根据官方提供的代码,我按照.netcore 的风格重构了代码;核心代码如下:
1、docker介绍 docker是用go语言编写基于linux操作系统的一些特性开发的,其提供了操作系统级别的抽象,是一种容器管理技术,它隔离了应用程序对基础架构(操作系统等)的依赖。相较于虚
demo运行在windows的docker中,系统是win10,所以需要先下载docker for windows,安装完毕后系统会重启,然后桌面上可以找到docker for windows的快捷
NetCore WebSocket 即时通讯示例,供大家参考,具体内容如下 1.新建Netcore Web项目 2.创建简易通讯协议 ?
需求场景: 我需要部署的项目是在Windows上开发的,目标框架为.net core 6.0 因此我们需要先在kylin上部署项目运行所需要的环境。 借助百
我正在 .NET Core 中重写一个调用外部 Web 服务的控制台应用程序。 我目前收到以下错误: One or more errors occurred. (The HTTP request is
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 6 年前。 Improve th
我有一组库,我想将其从 PCL 转移到 netcore。通过此举,我想简化 DI 系统并更新一些内部工作方式。 我想添加的其中一件事是内部对象的配置,就像在 Asp.Net Core 中一样(即 se
注:本文隶属于《理解ASP.NET Core》系列文章,请查看置顶博客或 点击此处查看全文目录 概述 在微服务化的架构设计中,网关扮演着重要的看门人角色,它所提供的功能之一
对于有多个应用系统的企业来说,每一个应用系统都有自己的用户体系,这就造成用户在切换不同应用系统时,就要多次输入账号密码,导致体验非常不好,也造成使用上非常不便。 针对这个问题,我们就可以采用单
就像 Web Api 接口可以对入参进行验证,避免用户传入非法的或者不符合我们预期的参数一样,选项也可以对配置源的内容进行验证,避免配置中的值与选项类中的属性不对应或者不满足预期,毕竟大部分配置
.NET Core 选项系统的主要实现在 Microsoft.Extensions.Options 和 Microsoft.Extensions.Options.ConfigurationExten
漏洞说明: 跨站脚本攻击(Cross Site Scripting),为了不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS。恶意攻击
分布式缓存是由多个应用服务器共享的缓存,通常作为访问它的应用服务器的外部服务进行维护。 分布式缓存可以提高 ASP.NET Core 应用的性能和可伸缩性,尤其是当应用由云服务或服务器场托管时。
一个应用要运行起来,往往需要读取很多的预设好的配置信息,根据约定好的信息或方式执行一定的行为。 配置的本质就是软件运行的参数,在一个软件实现中需要的参数非常多,如果我们以 Hard Code(
2. 配置添加 配置系统可以读取到配置文件中的信息,那必然有某个地方可以将配置文件添加到配置系统中。之前的文章中讲到 ASP.NET Core 入口文件中,builder(WebApplica
3. 配置提供程序 上面提到,通过 IConfigurationBuilder 的实现类对象,我们可以自由地往配置系统中添加不同的配置提供程序,从而获取不同来源的配置信息。.NET Core
4. 自定义配置提供程序 在 .NET Core 配置系统中封装一个配置提供程序关键在于提供相应的 IconfigurationSource 实现和 IConfigurationProvide
我是一名优秀的程序员,十分优秀!