簡體   English   中英

必須是具有公共無參數構造函數的非抽象類型 .net 核心

[英]must be a non-abstract type with a public parameterless constructor .net Core

我正在研究 .net Core 6 我已經搜索了這個錯誤,但我找不到解決方案

我有三個類,它們相互實現,但是當我開始我的 API 時,我從 EfProductDal class 收到錯誤。 它說 MyDbContext 必須是具有公共無參數構造函數的非抽象類型。

-基礎抽象 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
    {

    }

Mydb上下文:

public class MyDbContext:DbContext
    {
        public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) { }          
        public DbSet<Shipment> Shipments { get; set; }
        public DbSet<Product> Products { get; set; }
   }

請糾正我,如果有問題。 親切的問候...

where TContext: DbContext, new()表示 TContext 必須有一個默認的 c'tor。 MyDbContext沒有。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM