使用GetConnectionString()方法查找配置值,该查询的格式:ConnectionStrings:<connection string name>其中键是connection。 //var connection = "Data Source = {ip}; Initial Catalog = EFGetStarted.AspNetCore.NewDb; User ID = hsr;Password =js*2015;";varconnection = Configuration.GetConnectionString("Blogging...
_configuration.GetConnectionString("ReadOnlyDatabase") : _configuration.GetConnectionString("MainDatabase"); var optionsBuilder = new DbContextOptionsBuilder<MyDbContext>(); optionsBuilder.UseSqlServer(connectionString); return new MyDbContext(optionsBuilder.Options); } } 在应用程序中使用DBContext时,...
varconnectionString = builder.Configuration.GetConnectionString("DefaultConnection") ??thrownewInvalidOperationException("Connection string"+"'DefaultConnection' not found."); builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(connectionString)); ...
varconnectionString = builder.Configuration.GetConnectionString("DefaultConnection") ??thrownewInvalidOperationException("Connection string"+"'DefaultConnection' not found."); builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(connectionString)); ...
varconnectString = configuration.GetConnectionString(DefaultConnectionStringName); varbuilder =newDbContextOptionsBuilder<SunlightDbContext>(); builder.UseSqlServer(connectString); returnnewSunlightDbContext(builder.Options); } } 在了解这段代码之前,你可以先了解一下这个到底是做什么用的,就像注释里面说的,当...
publicpartialclassbyvarDBFirst: DbContext {//使用自定义连接串privatestaticstringGetEFConnctionString() {//string connString = "metadata=res://*/EFModel_FromDb.csdl|res://*/EFModel_FromDb.ssdl|res://*/EFModel_FromDb.msl;provider=System.Data.SqlClient;provider connection string=\"data source...
在处理这个问题一段时间后,根据文档,这是他们所建议的
using EFCoreExample.Models;using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); builder.Services.AddDbContext<CompanyContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));// Add services to the container.builder.Services....
DbContextInfo.ConnectionString 属性参考 反馈 定义命名空间: System.Data.Entity.Infrastructure 程序集: EntityFramework.dll 基础DbContext 类型使用的连接字符串。 C# 复制 public virtual string ConnectionString { get; } 属性值 String 适用于 产品版本 Entity Framework 4.3.1, 5.0.0, 6.2.0 ...
options.UseSqlServer(Configuration.GetConnectionString("BloggingDatabase"))); //其他服务配置... } 3. 上下文池与生存期 DbContext的生存期从创建实例时开始,并在释放实例时结束。在ASP.NET Core应用程序中,通常使用依赖关系注入为每个请求创建一个DbContext实例,并在请求结束后释放。