![](/img/trans.png)
[英]Unable to resolve service for type Microsoft.EntityFrameworkCore.DbContextOptions
[英]Unable to resolve service for type Microsoft.EntityFrameworkCore.DbContextOptions .net6
我正在使用 .net 6
當我將使用 VS 創建 Controller 時:
嘗試激活“Biblioteca.Data.Contexto”時無法解析服務類型“Microsoft.Entity.FrameworkCore.DbContextOptions”1[Biblioteca.Data.Contexto]。
那是我的 Model:
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Biblioteca.Models
{
[Table("Livro")]
public class Livro
{
[Display(Name="Id")]
[Column ("Id")]
[Key]
public int Id { get; set; }
[Display(Name = "Nome")]
[Column("Nome")]
[Required]
public string Nome { get; set; }
[Display(Name = "Arquivo")]
[Column("Livro")]
[Required]
public byte[] livro { get; set; }
}
}
這是我的背景:
using Biblioteca.Models;
using Microsoft.EntityFrameworkCore;
namespace Biblioteca.Data
{
public class Contexto :DbContext
{
public Contexto(DbContextOptions<Contexto> options) :base (options)
{ }
public DbSet<Livro> Livro { get; set; }
}
}
程序.cs:
using Biblioteca.Data;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
builder.Services.AddDbContext<Contexto>
(options => options.UseMySql(
"server=localhost;initial catalog=LIVRODB;uid:root;pwd:123",
Microsoft.EntityFrameworkCore.ServerVersion.Parse("8.0.30-mysql")));
我不認為您可以在執行builder.Build();
- 因此,您可能構建了沒有上下文的服務集合。
換個角度試試。 所以:
builder.Services.AddDbContext<Contexto>
(options => options.UseMySql(
"server=localhost;initial catalog=LIVRODB;uid:root;pwd:123",
Microsoft.EntityFrameworkCore.ServerVersion.Parse("8.0.30-mysql")));
var app = builder.Build();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.