設定內容以連線到 Microsoft SQL Server 資料庫。 C# publicstaticMicrosoft.EntityFrameworkCore.DbContextOptionsBuilderUseSqlServer(thisMicrosoft.EntityFrameworkCore.DbContextOptionsBuilder optionsBuilder, System.Data.Common.DbConnection connection,boolcontextOwnsConnection, Action<Microsoft.EntityFrameworkCore.In...
首先我们需要使用DI容器配置CatalogDbContext和OrderDbContext。您可以通过调用AddDbContext方法并指定正在配置的DbContext,然后使用SQL提供者特定方法传递连接字符串来实现。在这个例子中,我使用UseSqlServer方法连接到SQL Server。 using Microsoft.EntityFrameworkCore; services.AddDbContext<CatalogDbContext>(options => opt...
其他DbContext配置可以链接到Use*调用之前或之后(这不会有任何差别)。 例如,若要启用敏感数据日志记录: C#复制 publicclassApplicationDbContext:DbContext{protectedoverridevoidOnConfiguring(DbContextOptionsBuilder optionsBuilder){ optionsBuilder .EnableSensitiveDataLogging() .UseSqlServer(@"Server=(localdb)\mssqlloca...
"BloggingDatabase":"Server=(localdb)\\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" } } publicvoidConfigureServices(IServiceCollectionservices) { services.AddDbContext<BloggingContext>(options=> options.UseSqlServer(Configuration.GetConnectionString("BloggingDatabase"))); //其他服务配置... }...
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder){ if (!optionsBuilder.IsConfigured) { optionsBuilder.UseSqlServer(@"Server=vaio;Database=Company;Trusted_Connection=True;"); }} 在实际的生产过程中我们一般不使用上面这种方式添加链接字符串 ...
options.UseSqlServer("connectionString")) public enum ServiceLifetime { Singleton = 0, Scoped = 1, Transient = 2 } 1. 2. 3. 4. Singleton :整个应用程序生命周期以内只创建一个实例。 Scoped: 在同一个Scope内只初始化一个实例 ,可以理解为(每一个 request 级别只创建一个实例) ...
public class ApplicationDbContext : DbContext{protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder){optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test");}} 通过此模式,还可以轻松地通过DbContext构造函数传递配置(如连接字符串)。 例如: ...
Based on your description, it seems that the sample, which is a sql server sample instead of MySQL sample.Please try to use Pomelo.EntityFrameworkCore.MySQL.prettyprint Copy Install-Package Pomelo.EntityFrameworkCore.MySQL Then use ...
使用依赖关系注入配置ASP.NET Core应用程序。 可以使用Startup.cs的ConfigureServices方法中的AddDbContext将EF Core添加到此配置。 例如: publicvoidConfigureServices(IServiceCollection services){services.AddControllers();services.AddDbContext<TestContext>(options=>options.UseSqlServer("name=ConnectionStrings:DefaultConne...
configured to use one and only one database provider. (Different instances of aDbContextsubtype can be used with different database providers, but a single instance must only use one.) A database provider is configured using a specificUse*call. For example, to use the SQL Server database ...