title: IdentityUser generic repository when we create Controller in Asp.Net MVC Core
body: <p>Here is Repository pattern, Normally if I used <strong>ContextDb</strong> instead of <strong>IdentityContextDb</strong> ,it would have worked but i have to use Indentity for creating my Identificial things.</p>
  
  <p><strong>Core.Repository</strong></p>
  
  <pre><code>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&lt;TEntity, TContext&gt; : IRepository&lt;TEntity&gt; 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&lt;TEntity&gt; GetAll(Expression&lt;Func&lt;TEntity, bool&gt;&gt; filter = null)
      {
          using (var context = new TContext())
          {
              return filter == null ? context.Set&lt;TEntity&gt;().AsEnumerable() : context.Set&lt;TEntity&gt;().Where(filter).AsEnumerable();
          }
      }
  
      public TEntity GetById(int id)
      {
          using (var context = new TContext())
          {
              return context.Set&lt;TEntity&gt;().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();
          }
      }
  }
  }
  </code></pre>
  
  <p>again but :) when i inherit my context from IdentityContextDb, it wants generic type like bottom of this line and its problem for Repository.</p>
  
  <p>i'll share what is like an error when we call this Repo </p>
  
  <p><strong>DAL.Context</strong></p>
  
  <pre><code>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&lt;AppUser&gt;
      {
          public HysWebDb(DbContextOptions options) : base(options)
          {
  
          }
  
      }
  }
  </code></pre>
  
  <p>and now lets call in Controller</p>
  
  <p><strong>UI.Controller</strong></p>
  
  <pre><code>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&lt;AppUser&gt; _uManager;
          private readonly Repository&lt;AppUser,XyzDb&gt; _userRepo;
          public RegisterController(UserManager&lt;AppUser&gt; uManager, XyzDb database)
          {
              _database = database;
              _uManager = uManager;
  
          }
     }
  
  }
  </code></pre>
  
  <p>although we havent injected yet but ...</p>
  
  <blockquote>
    <p><strong>Error   CS0311</strong>    The type 'XYZ.DAL.Context.XyzDb' cannot be used as type parameter 'TContext' in the generic type or method
    'Repository'. There is no implicit reference
    conversion from 'XYZ.DAL.Context.XyzDb' to
    'Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext'.        ~\XYZ.UI\Controllers\Account\RegisterController.cs  <strong>21</strong>  Active</p>
    
    <p><strong>Error   CS0310</strong>    'XyzDb' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TContext'
    in the generic type or method 'Repository'        ~\XYZ.UI\Controllers\Account\RegisterController.cs  <strong>21</strong>  Active</p>
    
    <p>Line 21 is here  <strong>private readonly Repository
    _userRepo;</strong></p>
  </blockquote>
  
  <p>Thanks,</p>
  
  <p>Özgür</p>