I am working on .net Core 6 I have search this error but i couldn't find a solution
I have three classes and they are implement each other but while i am starting my APİ i am getting error from EfProductDal class. It saying MyDbContext must be non-abstract type with a public parameterless constructor.
-base abstract class
public class EfEntityRepositoryBase<TEntity, TContext>
: IEntityRepository<TEntity>
where TEntity : class, IEntity, new()
where TContext : DbContext, new()
{
public void Add(TEntity entity)
{
using (var context = new TContext())
{
var addedEntity = context.Entry(entity);
addedEntity.State = EntityState.Added;
context.SaveChanges();
}
}
}
-IEntityRepository
public interface IEntityRepository<T> where T : class, IEntity, new()
{
T Get(Expression<Func<T, bool>> filter);
IList<T> GetList(Expression<Func<T, bool>> filter = null);
void Add(T entity);
void Update(T entity);
void Delete(T entity);
}
-EfProductDal
public class EfProductDal : EfEntityRepositoryBase<Product, MyDbContext>, IProductDal
{
}
MydbContext:
public class MyDbContext:DbContext
{
public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) { }
public DbSet<Shipment> Shipments { get; set; }
public DbSet<Product> Products { get; set; }
}
Please, correct me, if something wrong. Kind Regards...
where TContext: DbContext, new()
says that TContext must have a default c'tor. But MyDbContext
does not.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.