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().ToTable("T_Product");
modelBuilder.Entity().ToTable("T_Order");
base.OnModelCreating(modelBuilder);
}

posted @ 2025-04-17 21:37  FZ1531  阅读(4)  评论(0)    收藏  举报