EF core学习记录(.netCore 数据库连接以及一些操作)
1.创建上下文连接池AppDbContext
作用:与数据库连接纽带之一,可以理解为创建了一个数据库名为AppDbContext
代码如下:
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
public DbSet<Employer> Employers { get; set; }
}
代码中可以这么理解:
AppDbContext 可以理解为数据库中的一个数据库
Employers可以理解为数据库下的一个表
通过构造函数配置,将配置文件传到父类进行配置连接
2.创建接口
原因,因为数据库可能不止一个,可能有好多个,这个时候就需要接口在startUp类里进行配置,前台应该调用的哪一个数据库
接口代码如下:
interface IEmployerRepository
{
/// <summary>
/// 通过ID获取学生信息
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
Employer GetEmployer(int id);
IEnumerable<Employer> getAllEmployers();
Employer Add(Employer employer);
Employer Update(Employer employer);
Employer Delete(int id);
}
StartUp类配置如下:
public void ConfigureServices(IServiceCollection services)
{
//services.AddPooledDbContextFactory<AppDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("EmployerConnection")));
services.AddDbContextPool<AppDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("EmployerConnection")));
services.AddScoped<IEmployerRepository,SQLEmployerRepository>(); //将接口与数据库绑定
services.AddControllersWithViews().AddRazorRuntimeCompilation();
}
其中这一句就是的
services.AddScoped<IEmployerRepository,SQLEmployerRepository>();
1
如果时间库换了的话,就只要更改SQLEmplyerRepository就可以了
3创建继承这个接口来查询对应数据库的类
代码如下
public class SQLEmployerRepository : IEmployerRepository
{
private readonly AppDbContext context;
public SQLEmployerRepository(AppDbContext context)
{
this.context = context;
}
public Employer Add(Employer employer)
{
context.Employers.Add(employer);
context.SaveChanges();
return employer;
}
public Employer Delete(int id)
{
Employer employer = context.Employers.Find(id);
if(employer != null)
{
context.Employers.Remove(employer);
context.SaveChanges();
}
return employer;
}
public IEnumerable<Employer> getAllEmployers()
{
return context.Employers;
}
public Employer GetEmployer(int id)
{
return context.Employers.Find(id);
}
public Employer Update(Employer UpdateEmployer)
{
var employer = context.Employers.Attach(UpdateEmployer);
employer.State = Microsoft.EntityFrameworkCore.EntityState.Modified;
context.SaveChanges();
return UpdateEmployer;
}
}
还需要配置一个重要环节
前面说要把配置传给父类进行配置,但是配置文件信息还没有配置过去,就需要到startup类中进行配置
代码如下
public IConfiguration Configuration { get; } //定义配置变量
public Startup(IConfiguration configuration)
{
Configuration = configuration;//实例话时传入的配置信息,即将Configuration获取了配置信息
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//services.AddPooledDbContextFactory<AppDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("EmployerConnection")));
services.AddDbContextPool<AppDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("EmployerConnection")));
services.AddScoped<IEmployerRepository,SQLEmployerRepository>(); //将接口与数据库绑定
services.AddControllersWithViews().AddRazorRuntimeCompilation();
}
其中下面这句(两句,一个池,一个工厂池)就是配置传递配置信息的中间件
//services.AddPooledDbContextFactory<AppDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("EmployerConnection")));
services.AddDbContextPool<AppDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("EmployerConnection")));
解释: services.AddDbContextPool
通过泛型将AppDbContext(数据库)进行连接配置
解释:options => options.UseSqlServer(Configuration.GetConnectionString(“EmployerConnection”))
告诉程序用SQLserver方式配置连接字符串,其中连接字符串是Configuration.GetConnectionString(“EmployerConnection”)
5配置连接字符串
在appSetting中进行配置,如下代码
{
"AllowedHosts": "*",
"ConnectionStrings": {
"EmployerConnection": "server=(localdb)\\MSSQLLocalDB;database=AppDbContext;Trusted_Connection=true"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
其中下面这一句就是的
"ConnectionStrings": {
"EmployerConnection": "server=(localdb)\\MSSQLLocalDB;database=EmployDb;Trusted_Connection=true"
},
6在Controller控制器中注入
代码如下,通过构造函数进行注入,这样就可以在控制器中使用了
private readonly IEmployerRepository employerRepository;
public ScanController(IEmployerRepository employer) => employerRepository = employer;