.net Core 调用EF Core 爬坑
public class MyDBContext:DbContext
{
public MyDBContext(DbContextOptions<MyDBContext> opt) :base(opt)
{
}
public DbSet<Product> Products { get; set; }
...
}
2、.netCore 中引用 Startup.cs中ConfigureServices 注入EF Core服务
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<Sport.Entity.SportStoreDBContext>(opt =>
opt.UseSqlServer(Configuration.GetConnectionString("SportDB"))//appsettings.json ConnectionStrings.SportDB
) ;
...
}
appsettings.json 中配置ConnectionStrings 节点值
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"SportDB": "Server=(localdb)\\ProjectsV13;database=MyPracticeDB;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
3 页面中调用,net Core 将EF服务,以构造函数的方式提供调用
private readonly SportStoreDBContext _sportContext;
public HomeController(SportStoreDBContext sportContext)
{
_sportContext = sportContext;
}
//调用
public ActionResult List()
{
var list = _sportContext.Product.ToList();
}
本文来自博客园,作者:jiayouliucui,转载请注明原文链接:https://www.cnblogs.com/cheery-go/p/15603554.html

浙公网安备 33010602011771号