Must be a non-abstract type with a public parameterless constructor c#

59 views Asked by At

Here is the my context class. I want to use my db takes the connection info from appsettings.json. I commented override method and added public CoursesystemContext but I got error.

namespace DataAccess.Concrete.EntityFreamwork.Contexts
{   
    public class CourseSystemContext : DbContext
    {
        public CourseSystemContext(DbContextOptions<CourseSystemContext>options): base(options)
        {

        }

        //protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        //{
            // optionsBuilder.UseSqlServer(connectionString: @"Server=(localdb)\MSSQLLocalDB;Database=Northwind;Trusted_Connection=true");
            //optionsBuilder.UseMySql(connectionString: @"server=localhost;userid=root;password=....;database=coursesystemdb");
        //}
    }
}

I added the json setting:

{
  "ConnectionStrings": {
    "connectionString": "server=localhost;userid=root;password=...;database=coursesystemdb"
}

and arranged startup.cs:

services.AddDbContext<CourseSystemContext>(options =>
            options.UseMySql(Configuration.GetConnectionString("connectionString")));

Then I debug and get this error:

Error CS0310 'CourseSystemContext' 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 'EfEntityRepositoryBase<TEntity, TContext>' DataAccess C:\Users\kayha\Desktop\langsisv3-api-main\langsisv3-api-main\CourseSystem\DataAccess\Concrete\EntityFreamwork\EfAgencyDal.cs 14

How can solve this error? Thanks a lot.

0

There are 0 answers