【发布时间】:2021-10-29 07:47:25
【问题描述】:
我有一个关于在控制器外使用数据库上下文的问题,即如何在常规类中调用数据库上下文? 为了与数据库通信,我使用:EF Core
我使用了这样的选项:
private readonly MSSQLContext _context;
public BookingController(MSSQLContext context)
{
_context = context;
}
另类
using (MSSQLContext context=new MSSQLContext())
{
context.get_Users.ToList();
}
Startup.cs
services.AddDbContext<MSSQLContext>(options =>
options.UseSqlServer(connection));
MSSQLContext.cs
public MSSQLContext()
{
}
public MSSQLContext(DbContextOptions<MSSQLContext> options)
: base(options)
{
}
public DbSet<VIVT_Core_Aud.Models.Core.Logger_Model> Logger_Models { get; set; }
还有更多表格...
【问题讨论】:
标签: c# sql-server entity-framework asp.net-core