[英]Unable to resolve service for type Microsoft.EntityFrameworkCore.DbContextOptions .net6
i'm Using .net 6我正在使用 .net 6
when i will create a Controller using the VS:当我将使用 VS 创建 Controller 时:
Unable to resolve service type "Microsoft.Entity.FrameworkCore.DbContextOptions" 1[Biblioteca.Data.Contexto] while attempting to activate 'Biblioteca.Data.Contexto'.'尝试激活“Biblioteca.Data.Contexto”时无法解析服务类型“Microsoft.Entity.FrameworkCore.DbContextOptions”1[Biblioteca.Data.Contexto]。
That is my Model:那是我的 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; }
}
}
That is my context:这是我的背景:
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; }
}
}
Program.cs:程序.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")));
I don't think you can add additional services after doing builder.Build();
我不认为您可以在执行
builder.Build();
- So you probably build the service collection without the context in it. - 因此,您可能构建了没有上下文的服务集合。
Try switching it around.换个角度试试。 So:
所以:
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.