Web Api学习收录
1.要想使Programs里面的builder.Services.AddDbContext<>(options=>options.useMysql())生效,需要在自建 xxxDbContext的构造函数继承DbContext的构造函数
public BingDbContext(DbContextOptions options) : base(options)
2.要想在xxxController使用xxxDbContext的依赖注入,需要使用构造函数
private BingDbContext _bingDbContext;
public ProductController(BingDbContext bingDbContext)
{
_bingDbContext = bingDbContext;
}
3.Webapi里面想要定义表名的话,可以覆盖 OnModelCreating方法,调用modelBuilder.Entity<>().ToTable()方法
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity
modelBuilder.Entity
base.OnModelCreating(modelBuilder);
}