简体   繁体   English

无法解析 Microsoft.EntityFrameworkCore.DbContextOptions .net6 类型的服务

[英]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.

相关问题 无法解析 Microsoft.EntityFrameworkCore.DbContextOptions 类型的服务 - Unable to resolve service for type Microsoft.EntityFrameworkCore.DbContextOptions 无法解析类型“Microsoft.EntityFrameworkCore.DbContextOptions”的服务 - Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions' '在尝试激活时无法解析类型 ¨Microsoft.entityFrameworkCore.DbContextOptions¨1[LibraryData.LibraryContext] 的服务 - 'Unable to resolve service for type ¨Microsoft.entityFrameworkCore.DbContextOptions¨1[LibraryData.LibraryContext] while attempting to activate .Net 6 无法解析类型为 Microsoft.EntityFrameworkCore.DbSet 的服务 - .Net 6 Unable to resolve service for type Microsoft.EntityFrameworkCore.DbSet 参数 1:无法从“Microsoft.EntityFrameworkCore.DbContextOptions”转换为“字符串” - Argument 1: cannot convert from 'Microsoft.EntityFrameworkCore.DbContextOptions' to 'string' ASP.NET Core 2 无法解析 Microsoft EntityFrameworkCore DbContext 类型的服务 - ASP.NET Core 2 Unable to resolve service for type Microsoft EntityFrameworkCore DbContext 我该如何解决这个错误? 参数 1:无法将“字符串”转换为“Microsoft.EntityFrameworkCore.DbContextOptions” - How do I fix this error? Argument 1: cannot convert 'string' to 'Microsoft.EntityFrameworkCore.DbContextOptions' C# DbContext 问题:参数 1:无法将“字符串”转换为“Microsoft.EntityFrameworkCore.DbContextOptions” - C# Problem with DbContext: Argument 1: cannot convert 'string' to 'Microsoft.EntityFrameworkCore.DbContextOptions' Generating CRUD pages in ASP.NET 6 Razor 页面以错误“Unable to resolve service for type DbContextOptions”结束 - Generating CRUD pages in ASP.NET 6 Razor Pages ends with an error "Unable to resolve service for type DbContextOptions" Azure 应用服务实体框架:无法创建新的 controller:无法解析 dbcontextoptions 类型的服务 - Azure App Service Entity Framework: Cannot create new controller: unable to resolve service for type dbcontextoptions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM