Skip to content

Commit

Permalink
Merge pull request #14276 from abpframework/MyProjectNameDbContextFac…
Browse files Browse the repository at this point in the history
…tory-6.0

Add `MyProjectNameDbContextFactory` to `nolayers` project.
  • Loading branch information
realLiangshiwei authored Oct 10, 2022
2 parents 11d35d5 + e4d8c37 commit f002253
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;

namespace MyCompanyName.MyProjectName.Data;

public class MyProjectNameDbContextFactory : IDesignTimeDbContextFactory<MyProjectNameDbContext>
{
public MyProjectNameDbContext CreateDbContext(string[] args)
{
//<TEMPLATE-REMOVE IF-NOT='dbms:PostgreSQL'>
// https://www.npgsql.org/efcore/release-notes/6.0.html#opting-out-of-the-new-timestamp-mapping-logic
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
//</TEMPLATE-REMOVE>

var configuration = BuildConfiguration();

var builder = new DbContextOptionsBuilder<MyProjectNameDbContext>()
.UseSqlServer(configuration.GetConnectionString("Default"));

return new MyProjectNameDbContext(builder.Options);
}

private static IConfigurationRoot BuildConfiguration()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false);

return builder.Build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;

namespace MyCompanyName.MyProjectName.Data;

public class MyProjectNameDbContextFactory : IDesignTimeDbContextFactory<MyProjectNameDbContext>
{
public MyProjectNameDbContext CreateDbContext(string[] args)
{
//<TEMPLATE-REMOVE IF-NOT='dbms:PostgreSQL'>
// https://www.npgsql.org/efcore/release-notes/6.0.html#opting-out-of-the-new-timestamp-mapping-logic
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
//</TEMPLATE-REMOVE>

var configuration = BuildConfiguration();

var builder = new DbContextOptionsBuilder<MyProjectNameDbContext>()
.UseSqlServer(configuration.GetConnectionString("Default"));

return new MyProjectNameDbContext(builder.Options);
}

private static IConfigurationRoot BuildConfiguration()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false);

return builder.Build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;

namespace MyCompanyName.MyProjectName.Data;

public class MyProjectNameDbContextFactory : IDesignTimeDbContextFactory<MyProjectNameDbContext>
{
public MyProjectNameDbContext CreateDbContext(string[] args)
{
//<TEMPLATE-REMOVE IF-NOT='dbms:PostgreSQL'>
// https://www.npgsql.org/efcore/release-notes/6.0.html#opting-out-of-the-new-timestamp-mapping-logic
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
//</TEMPLATE-REMOVE>

var configuration = BuildConfiguration();

var builder = new DbContextOptionsBuilder<MyProjectNameDbContext>()
.UseSqlServer(configuration.GetConnectionString("Default"));

return new MyProjectNameDbContext(builder.Options);
}

private static IConfigurationRoot BuildConfiguration()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false);

return builder.Build();
}
}

0 comments on commit f002253

Please sign in to comment.