asp.net core 连接数据库
两种方式:
界面简介
1、固定式
引用类库
Domain 项目下有个 ApplicationDbContext.cs文件
public class ApplicationDbContext:DbContext { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json"); var configuration = builder.Build(); var conn = configuration.GetConnectionString("JConnection"); optionsBuilder.UseSqlServer(conn); } public DbSet<TB_User> tB_Users { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); } }
WebLearn2下载的appSettings.json
应用:
HomeController中的Index 方法
public IActionResult Index() { using (ApplicationDbContext applicationDbContext = new ApplicationDbContext()) { var ieumberable = applicationDbContext.Set<TB_User>().Find(1); ViewBag.Name = ieumberable.Name; } return View(); }
页面效果:
2、注入式
1)第一种方式
ApplicationDbContext.cs 文件代码
public class ApplicationDbContext:DbContext { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json"); var configuration = builder.Build(); var conn = configuration.GetConnectionString("JConnection"); optionsBuilder.UseSqlServer(conn); } public DbSet<TB_User> tB_Users { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); } }
startup.cs文件
public void ConfigureServices(IServiceCollection services) { services.AddEntityFrameworkSqlServer().AddDbContext<ApplicationDbContext>(options => { options.UseSqlServer(Configuration.GetConnectionString("JConnection")); }); services.AddScoped<DbContext, ApplicationDbContext>(); }
homeController.cs 写法
private readonly DbContext _dbContext; public HomeController(ILogger<HomeController> logger,Pic_UserIDAL pic_UserIDAL, DbContext dbContext) { _logger = logger; _pic_UserIDAL = pic_UserIDAL; _dbContext = dbContext; } public IActionResult Index() { var pic = this._dbContext.Set<Tb_User>().Find(1); return View(); }
2)第二种方式
Startup.cs
public void ConfigureServices(IServiceCollection services) { //services.AddScoped<DbContext, ApplicationDbContext>(); services.AddDbContext<ApplicationDbContext>(options=>options.UseSqlServer(Configuration.GetConnectionString("JConnection"))); //services.AddEntityFrameworkSqlServer().AddDbContext<ApplicationDbContext>(options => //{ // options.UseSqlServer(Configuration.GetConnectionString("JConnection")); //}); //services.AddDbContext<ApplicationDbContext>(options=> // options.UseSqlServer(Configuration.GetConnectionString("ConnectionStrings:BaseDb")) //); services.AddScoped<Pic_UserIDAL, Pic_UserDAL>(); //services.AddScoped<Pic_UserIDAL, Pic_UserDAL>(); services.AddControllersWithViews(); }
ApplicationDbContext.cs文件
public class ApplicationDbContext:DbContext { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } //protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) //{ // var builder = new ConfigurationBuilder() // .SetBasePath(Directory.GetCurrentDirectory()) // .AddJsonFile("appsettings.json"); // var configuration = builder.Build(); // var conn = configuration.GetConnectionString("JConnection"); // optionsBuilder.UseSqlServer(conn); //} public DbSet<TB_User> tB_Users { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); } }
HomeController.cs文件
private readonly ApplicationDbContext _dbContext; public HomeController(ILogger<HomeController> logger,Pic_UserIDAL pic_UserIDAL, ApplicationDbContext dbContext) { _logger = logger; _pic_UserIDAL = pic_UserIDAL; _dbContext = dbContext; } public IActionResult Index() { var pic = this._dbContext.Set<Pic_User>().Find(1); return View(); }