에 있습니다.NetCore 콘솔에서 주입 모드로 데이터베이스에 액세스

1700 단어 C#
직접 부호:
Program.cs
private static readonly IConfigurationBuilder Configuration = new ConfigurationBuilder();
private static IConfigurationRoot _configuration;
private static SqlDBContext _dbContext;//      DBContext
public static void Main(string[] args)
{
 _configuration = Configuration.SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddEnvironmentVariables().Build();
            var serviceCollection = new ServiceCollection()
                .AddDbContextPool(options =>
                    {
                        options.UseSqlServer(_configuration.GetConnectionString("check"), //             
                        b => b.UseRowNumberForPaging());
                    })
                .AddTransient()                
                .AddOptions();
            var provider = serviceCollection.BuildServiceProvider();
           _dbContext = provider.GetService();
           //       DBContext    
            AreasDAL areasDAL=new AreasDAL(_dbContext);
}

AreasDAL 
public class AreasDAL
{
    private SqlDBContext.SqlDBContext _sqlDBContext;
    public AreasDAL(SqlDBContext.SqlDBContext sqlDBContext)
        {
            _sqlDBContext = sqlDBContext;
        }
    //                
}

SqlDBContext
public class SqlDBContext : DbContext
{
    public SqlDBContext(DbContextOptions options) : base(options)
     {

     }
    public DbSet DbAreas { get; set; }
}

좋은 웹페이지 즐겨찾기