この質問は何度か尋ねられましたが、私の解決策はまだありませんでした。DotNetコア、データベースプロバイダがこのDbContext用にコンフィグレーションされていません
私はDotNet Coreで非常に新しいです。 私の問題は、単純なlinqコマンドを使用して "Header"テーブルからデータを選択したいのですが、私は以下のエラーに直面しました。
追加情報: このDbContextに対してデータベースプロバイダが設定されていません。プロバイダーは、 DbContext.OnConfiguringメソッドをオーバーライドするか、 アプリケーションサービスプロバイダーでAddDbContextを使用して構成できます。 AddDbContextが使用されている場合は、 もDbContext型が のDbContextOptionsオブジェクトをコンストラクタとして受け入れ、DbContextの基本コンストラクタに渡していることを確認してください。
マイコンがあります。
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;
}
}
My ApplicationDbContextは、
public class ApplicationDbContext : IdentityDbContext<ApplicationUser> //DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public ApplicationDbContext() : base()
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}
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();
ありがとうございました。
ご回答いただきありがとうございます。使用ブロックのdbContextを新規作成し、そのDbSet <>を使用してデータを選択、追加、削除します。あなたのソリューションは問題ありませんが、私の問題は解決しませんでした。 –
コントローラではないクラスのコンストラクタにdbcontextを注入するにはどうすればよいですか? – zuckerthoben
@zuckerthobenあなたはその質問[ここ](https://stackoverflow.com/questions/37189984/dependency-injection-with-classes-other-than-a-controller-class)の答えがあります:) – 5ar