Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当我们在Asp.Net MVC核心中创建控制器时,IdentityUser通用存储库_C#_Asp.net Mvc_Asp.net Core_Asp.net Identity_Repository Pattern - Fatal编程技术网

C# 当我们在Asp.Net MVC核心中创建控制器时,IdentityUser通用存储库

C# 当我们在Asp.Net MVC核心中创建控制器时,IdentityUser通用存储库,c#,asp.net-mvc,asp.net-core,asp.net-identity,repository-pattern,C#,Asp.net Mvc,Asp.net Core,Asp.net Identity,Repository Pattern,这是存储库模式,通常情况下,如果我使用ContextDb而不是IdentityContextDb,它会起作用,但我必须使用标识来创建标识性的东西 Core.Repository using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq;

这是存储库模式,通常情况下,如果我使用ContextDb而不是IdentityContextDb,它会起作用,但我必须使用标识来创建标识性的东西

Core.Repository

using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;



namespace XYZ.CORE.Repository
 {
     public class Repository<TEntity, TContext> : IRepository<TEntity> where 
 TEntity : class, new() where TContext : IdentityDbContext, new()
     {
         public void Delete(TEntity entity)
         {
             using (var context=new TContext())
             {
                 var deleteEntity = context.Entry(entity);
                 deleteEntity.State = EntityState.Deleted;
                 context.SaveChanges();
             }
         }

     public IEnumerable<TEntity> GetAll(Expression<Func<TEntity, bool>> filter = null)
    {
        using (var context = new TContext())
        {
            return filter == null ? context.Set<TEntity>().AsEnumerable() : context.Set<TEntity>().Where(filter).AsEnumerable();
        }
    }

    public TEntity GetById(int id)
    {
        using (var context = new TContext())
        {
            return context.Set<TEntity>().Find(id);
        }
    }

    public void Insert(TEntity entity)
    {
        using (var context = new TContext())
        {
            var addEntity = context.Entry(entity);
            addEntity.State = EntityState.Added;
            context.SaveChanges();
        }
    }

    public void Update(TEntity entity)
    {
        using (var context = new TContext())
        {
            var updatedEntity = context.Entry(entity);
            updatedEntity.State = EntityState.Modified;
            context.SaveChanges();
        }
    }
}
}
使用Microsoft.AspNetCore.Identity.EntityFrameworkCore;
使用Microsoft.EntityFrameworkCore;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Linq.Expressions;
使用系统文本;
命名空间XYZ.CORE.Repository
{
公共类存储库:IRepository where
TEntity:class,new(),其中TContext:IdentityDbContext,new()
{
公共作废删除(可撤销实体)
{
使用(var context=new TContext())
{
var deleteEntity=context.Entry(实体);
deleteEntity.State=EntityState.Deleted;
SaveChanges();
}
}
公共IEnumerable GetAll(表达式筛选器=null)
{
使用(var context=new TContext())
{
返回筛选器==null?context.Set().AsEnumerable():context.Set().Where(filter.AsEnumerable();
}
}
公共TEntity GetById(内部id)
{
使用(var context=new TContext())
{
返回context.Set().Find(id);
}
}
公共无效插入(TEntity实体)
{
使用(var context=new TContext())
{
var addEntity=上下文条目(实体);
addEntity.State=EntityState.Added;
SaveChanges();
}
}
公共无效更新(TEntity实体)
{
使用(var context=new TContext())
{
var updatedEntity=context.Entry(实体);
updatedEntity.State=EntityState.Modified;
SaveChanges();
}
}
}
}
但是:)当我从IdentityContextDb继承我的上下文时,它需要像这行底部这样的泛型类型,以及它在存储库中的问题

我将分享我们称之为回购的错误

DAL.Context

using XYZ.DATA.Entity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;

namespace XYZ.DAL.Context
{
   public class XyzDb:IdentityDbContext<AppUser>
    {
        public HysWebDb(DbContextOptions options) : base(options)
        {

        }

    }
}
使用XYZ.DATA.Entity;
使用Microsoft.AspNetCore.Identity.EntityFrameworkCore;
使用Microsoft.EntityFrameworkCore;
使用制度;
使用System.Collections.Generic;
使用系统文本;
命名空间XYZ.DAL.Context
{
公共类XyzDb:IdentityDbContext
{
public HysWebDb(DbContextOptions选项):基本(选项)
{
}
}
}
现在让我们调用控制器

UI.Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using XYZ.UI.Models.VM;
using Microsoft.AspNetCore.Mvc;
using XYZ.DAL.Context;
using Microsoft.AspNetCore.Identity;
using XYZ.DATA.Entity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.AspNetCore.Authorization;
using XYZ.CORE.Repository;

namespace XYZ.UI.Controllers.Account
{
     //[Authorize(Roles ="Admin")]
    public class RegisterController : Controller
    {
        private readonly XyzDb _database;
        private readonly UserManager<AppUser> _uManager;
        private readonly Repository<AppUser,XyzDb> _userRepo;
        public RegisterController(UserManager<AppUser> uManager, XyzDb database)
        {
            _database = database;
            _uManager = uManager;

        }
   }

}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用XYZ.UI.Models.VM;
使用Microsoft.AspNetCore.Mvc;
使用XYZ.DAL.Context;
使用Microsoft.AspNetCore.Identity;
使用XYZ.DATA.Entity;
使用Microsoft.AspNetCore.Identity.EntityFrameworkCore;
使用Microsoft.AspNetCore.Authorization;
使用XYZ.CORE.Repository;
命名空间XYZ.UI.Controllers.Account
{
//[授权(Roles=“Admin”)]
公共类注册控制器:控制器
{
专用只读XyzDb_数据库;
私有只读用户管理器;
私有只读存储库_userRepo;
公共注册表控制器(UserManager uManager,XyzDb数据库)
{
_数据库=数据库;
_uManager=uManager;
}
}
}
虽然我们还没有注射但是

错误CS0311类型“XYZ.DAL.Context.XyzDb”不能用作泛型类型或方法中的类型参数“TContext” “存储库”。没有隐式引用 从“XYZ.DAL.Context.XyzDb”到 “Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext”~\XYZ.UI\Controllers\Account\RegisterController.cs21活动

错误CS0310“XyzDb”必须是具有公共无参数构造函数的非抽象类型,才能将其用作参数“TContext” 在“Repository”的泛型类型或方法中~\XYZ.UI\Controllers\Account\RegisterController.cs21处于活动状态

第21行在这里私有只读存储库 _用户回购

谢谢


Özgür

实际上,您正在对泛型存储库类设置一个约束

public class Repository<TEntity, TContext> : IRepository<TEntity> where
TEntity : class, new() where TContext : IdentityDbContext, new()
公共类存储库:IRepository where
TEntity:class,new(),其中TContext:IdentityDbContext,new()
这是IdentityDbContext上的new(),因此IdentityDbContext应该包含一个无参数构造函数。
有关更多详细信息,请查看microsoft docs

实际上,您正在对通用存储库类设置约束

public class Repository<TEntity, TContext> : IRepository<TEntity> where
TEntity : class, new() where TContext : IdentityDbContext, new()
公共类存储库:IRepository where
TEntity:class,new(),其中TContext:IdentityDbContext,new()
这是IdentityDbContext上的new(),因此IdentityDbContext应该包含一个无参数构造函数。 有关更多详细信息,请查看microsoft文档

公共类存储库:IRepository where
TEntity:class,new(),其中TContext:IdentityDbContext,new()
您在这里定义了两件事:

  • repository类必须使用class there tenty&TContext has new(),这意味着无参数构造函数,而实际上只有一个带选项的构造函数-(这是您的第二个错误)
  • public HysWebDb(DbContextOptions选项):基本(选项)

  • 第二个问题不是100%确定的,但是如果您继承了一个泛型接口,但声明了类型-IdentityDbContext-它不能被认为是唯一的IdentityDbContext
  • 公共类XyzDb:IdentityDbContext

    要解决您的问题,您应该将公共类存储库更改为:

     public class Repository<TEntity, TContext> : IRepository<TEntity> where 
     TEntity : class, new() where TContext : IdentityDbContext<AppUser>
    
    公共类存储库:IRepository where
    tenty:class,new(),其中TContext:IdentityDbContext
    
    公共类存储库:IRepository where