简体   繁体   English

ASP.Net Core - 尝试激活 *Controller 时无法解析 *Context 类型的服务

[英]ASP.Net Core - Unable to resolve service for type *Context while attempting to activate *Controller

Total newb here.总新手在这里。 I feel I'm getting close to understanding, but most articles revolve around Startup.cs which I don't have.我觉得我已经接近理解了,但是大多数文章都围绕着我没有的 Startup.cs。 I am stuck on error:我陷入了错误:

InvalidOperationException: Unable to resolve service for type 'SFTP.Models.SFTPContext' while attempting to activate 'SFTP.Controllers.ClientInfsController'. InvalidOperationException:尝试激活“SFTP.Controllers.ClientInfsController”时无法解析“SFTP.Models.SFTPContext”类型的服务。

What am I missing?我错过了什么?

Program.cs程序.cs

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

Controller - HomeController.cs Controller - HomeController.cs

using Microsoft.AspNetCore.Mvc;
using SFTP.Models;
using System.Diagnostics;

namespace SFTP.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}

Controller - ClienInfsContoller.cs Controller - ClienInfsContoller.cs

#nullable disable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using SFTP.Models;

namespace SFTP.Controllers
{
    public class ClientInfsController : Controller
    {
        private readonly SFTPContext _context;

        public ClientInfsController(SFTPContext context)
        {
            _context = context;
        }

        // GET: ClientInfs
        public async Task<IActionResult> Index()
        {
            return View(await _context.ClientInfs.ToListAsync());
        }

        // GET: ClientInfs/Details/5
        public async Task<IActionResult> Details(string id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var clientInf = await _context.ClientInfs
                .FirstOrDefaultAsync(m => m.Limiters == id);
            if (clientInf == null)
            {
                return NotFound();
            }

            return View(clientInf);
        }

        // GET: ClientInfs/Create
        public IActionResult Create()
        {
            return View();
        }

        // POST: ClientInfs/Create
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Client,Inpath,Outpath,Limiters")] ClientInf clientInf)
        {
            if (ModelState.IsValid)
            {
                _context.Add(clientInf);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(clientInf);
        }

        // GET: ClientInfs/Edit/5
        public async Task<IActionResult> Edit(string id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var clientInf = await _context.ClientInfs.FindAsync(id);
            if (clientInf == null)
            {
                return NotFound();
            }
            return View(clientInf);
        }

        // POST: ClientInfs/Edit/5
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(string id, [Bind("Client,Inpath,Outpath,Limiters")] ClientInf clientInf)
        {
            if (id != clientInf.Limiters)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(clientInf);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ClientInfExists(clientInf.Limiters))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            return View(clientInf);
        }

        // GET: ClientInfs/Delete/5
        public async Task<IActionResult> Delete(string id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var clientInf = await _context.ClientInfs
                .FirstOrDefaultAsync(m => m.Limiters == id);
            if (clientInf == null)
            {
                return NotFound();
            }

            return View(clientInf);
        }

        // POST: ClientInfs/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(string id)
        {
            var clientInf = await _context.ClientInfs.FindAsync(id);
            _context.ClientInfs.Remove(clientInf);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        private bool ClientInfExists(string id)
        {
            return _context.ClientInfs.Any(e => e.Limiters == id);
        }
    }
}

Model - SFTPContext.cs Model - SFTPContext.cs

using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using SFTP.Models;

namespace SFTP.Models
{
    public partial class SFTPContext : DbContext
    {
        public SFTPContext()
        {
        }

        public SFTPContext(DbContextOptions<SFTPContext> options)
            : base(options)
        {
        }

        public virtual DbSet<ClientInf> ClientInfs { get; set; } = null!;
        public virtual DbSet<SftpFileTrack> SftpFileTracks { get; set; } = null!;

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.
                optionsBuilder.UseSqlServer("Data Source=**********;Initial Catalog=SFTP;Integrated Security=True");
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<ClientInf>(entity =>
            {
                entity.HasKey(e => e.Limiters)
                    .HasName("PK__Client_I__7EFD3A29C824FBB5");

                entity.ToTable("Client_Inf");

                entity.Property(e => e.Limiters)
                    .HasMaxLength(255)
                    .IsUnicode(false)
                    .HasColumnName("LIMITERS");

                entity.Property(e => e.Client)
                    .HasMaxLength(255)
                    .IsUnicode(false)
                    .HasColumnName("CLIENT");

                entity.Property(e => e.Inpath)
                    .HasMaxLength(255)
                    .IsUnicode(false)
                    .HasColumnName("INPATH");

                entity.Property(e => e.Outpath)
                    .HasMaxLength(255)
                    .IsUnicode(false)
                    .HasColumnName("OUTPATH");
            });

            modelBuilder.Entity<SftpFileTrack>(entity =>
            {
                entity.HasKey(e => e.Infile)
                    .HasName("PK__SFTP_Fil__3B2253A7C9D06D81");

                entity.ToTable("SFTP_FileTrack");

                entity.Property(e => e.Infile)
                    .HasMaxLength(255)
                    .IsUnicode(false)
                    .HasColumnName("INFILE");

                entity.Property(e => e.Client)
                    .HasMaxLength(255)
                    .IsUnicode(false);

                entity.Property(e => e.Infilepath)
                    .HasMaxLength(255)
                    .IsUnicode(false)
                    .HasColumnName("INFILEPath");

                entity.Property(e => e.Intime)
                    .HasColumnType("datetime")
                    .HasColumnName("INTime");

                entity.Property(e => e.IntimeConfirm)
                    .HasColumnType("datetime")
                    .HasColumnName("INTimeCONFIRM");

                entity.Property(e => e.Outfile)
                    .HasMaxLength(255)
                    .IsUnicode(false)
                    .HasColumnName("OUTFILE");

                entity.Property(e => e.Outfilepath)
                    .HasMaxLength(255)
                    .IsUnicode(false)
                    .HasColumnName("OUTFILEPath");

                entity.Property(e => e.Outtime)
                    .HasColumnType("datetime")
                    .HasColumnName("OUTTime");

                entity.Property(e => e.Status)
                    .HasMaxLength(50)
                    .IsUnicode(false)
                    .HasColumnName("STATUS");
            });

            OnModelCreatingPartial(modelBuilder);
        }

        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
    }
}

Model - ClientInf.cs Model - ClientInf.cs

using System;
using System.Collections.Generic;

namespace SFTP.Models
{
    public partial class ClientInf
    {
        public string? Client { get; set; }
        public string? Inpath { get; set; }
        public string? Outpath { get; set; }
        public string Limiters { get; set; } = null!;
    }
}

Look like you are missing out inject SFTPContext which is the DbContext to DI container.看起来你错过了注入SFTPContext ,它是DbContext到 DI 容器。

Program.cs程序.cs

builder.Services.AddDbContext<SFTPContext>(options =>
   options.UseSqlServer(builder.Configuration.GetConnectionString(/* ConectionString key from appsettings.json */)));

Reference参考

Dependency injection (services) - ASP.NET Core fundamentals | 依赖注入(服务) - ASP.NET 核心基础 | Microsoft Docs 微软文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET 核心依赖注入错误 - 尝试激活“服务”时无法解析“存储库”类型的服务 - ASP.NET Core Dependency Injection error - Unable to resolve service for type “Repository” while attempting to activate “Service” ASP.NET Core通用主机(HostBuilder)尝试激活时无法解析类型…的服务 - ASP.NET Core Generic Host (HostBuilder) Unable to resolve service for type … while attempting to activate ASP.NET Core InvalidOperationException:尝试激活UserStore时无法解析DbContext类型的服务 - ASP.NET Core InvalidOperationException: Unable to resolve service for type DbContext while attempting to activate UserStore ASP.NET Core 错误:尝试激活时无法解析类型服务 - ASP.NET Core error: Unable to resolve service for type while attempting to activate 依赖注入错误:尝试激活时无法解析类型的服务,而 class 已注册 - Dependency Injection error: Unable to resolve service for type while attempting to activate, while class is registered ASP.NET 核心 - System.InvalidOperationException:尝试激活时无法解析服务类型 - ASP.NET Core - System.InvalidOperationException: Unable to resolve service for type while attempting to activate .net Core 6 - 尝试激活时无法解析服务类型 - .net Core 6 - Unable to resolve service for type while attempting to activate NET Core InvalidOperationException:尝试激活时无法解析类型服务 - NET Core InvalidOperationException: Unable to resolve service for type while attempting to activate ASP.NET CORE:尝试激活“API.Controllers.UsersController”时无法解析“API.SQLConnection.IDBConnection”类型的服务 - ASP.NET CORE : Unable to resolve service for type 'API.SQLConnection.IDBConnection' while attempting to activate 'API.Controllers.UsersController' ASP.NET Core Web API - 尝试激活“AuthService”时无法解析“Serilog.ILogger”类型的服务 - ASP.NET Core Web API - Unable to resolve service for type 'Serilog.ILogger' while attempting to activate 'AuthService'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM