我们可以通过依赖注入的方式 获得数据库上下文
然后就可以调用EF的接口 来处理数据的增删改查操作了。
下面 我们来看一下。
private MyDBContent myDBContent;
public HomeController(ILogHelperService logHelperService,IOptions<WebSettings> webOptions, IOptions<MySettings> myOptions,MyDBContent myDBContent)
{
this.webOptions = webOptions.Value;
this.myDBContent = myDBContent;Code language: HTML, XML (xml)
然后调用
public IActionResult TestDB()
{
myDBContent.Users.Add(new UserInfo() { Name = "黄老师", Password = "123" });
myDBContent.SaveChanges();
return Content("成功");
}Code language: PHP (php)
Previous: 41-EF Core数据升级
Next: 43-迁移数据库补充