【发布时间】:2017-09-02 01:47:26
【问题描述】:
我想使用一个简单的 LINQ 命令从我的表“标题”中选择数据,但我遇到了错误。
我的行动
public HeaderModel GetHeaderInformation()
{
using(var context = new ApplicationDbContext())
{
var header = context.Headers.Select(x => new HeaderModel
{
colorCode = x.colorCode,
height = x.height,
Id = x.Id,
left = x.left,
top = x.top,
width = x.width
}).FirstOrDefault();
return header;
}
}
错误
附加信息:未配置任何数据库提供程序 这个 DbContext。可以通过覆盖来配置提供程序 DbContext.OnConfiguring 方法或通过在上使用 AddDbContext 应用服务提供商。如果使用了 AddDbContext,那么也 确保您的 DbContext 类型接受 DbContextOptions 对象 它的构造函数并将其传递给 DbContext 的基本构造函数。
我的 ApplicationDbContext
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options) { }
public ApplicationDbContext() : base() { }
public DbSet<Header> Headers { get; set; }
public DbSet<Menu> Menus { get; set; }
}
我的 Startup.cs
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddApplicationInsightsTelemetry(Configuration);
services.AddIdentity<ApplicationUser, IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();
services.AddMvc();
提前致谢。
【问题讨论】:
标签: c# linq .net-core entity-framework-core dbcontext