0

I am new to ASP.NET Core. learning new version of .NET Core 2.0 using VS Code. I got stuck while doing creating database using migration. First, it gives an exception of implementation of IDesignTimeDbContextFactory. After solving this, it still gives an exception of

No parameterless constructor defined for this object

Here's my code for DbContextClass:

public VegaDbContext CreateDbContext(string[] args)
{
    IConfigurationRoot configuration = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json")
        .Build();
    var builder = new DbContextOptionsBuilder<VegaDbContext>();
    var connectionString = 
    configuration.GetConnectionString("DefaultConnection");
    builder.UseSqlServer(connectionString);
    return new VegaDbContext(builder.Options);
}
James Z
  • 12,209
  • 10
  • 24
  • 44
Malik Basit
  • 191
  • 2
  • 10
  • check this way https://stackoverflow.com/questions/48758249/ef-core-migration-cant-use-secret-manager – Neville Nazerane Feb 18 '18 at 19:43
  • Error message is pretty clean: Your models must have a parameterless constructor. Also don't read the config file every single time, it will create issues. https://stackoverflow.com/a/45876217/455493 on how to create a more abstract config reader or simply just use the DI and AddDbContext – Tseng Feb 18 '18 at 20:54
  • @Tseng my model already have parameterless constructor. On other hand configuration file is not creating issue. I already try what ever all of you people are suggesting but i don't understand where problem is. – Malik Basit Feb 19 '18 at 16:31
  • This problem is discussed here: https://github.com/aspnet/EntityFrameworkCore/issues/9467 – majjam Sep 13 '18 at 09:40

4 Answers4

1

I had tried a couple of ways when I was experimenting with ef core. I faced similar issues too. Finally I found services working great. First you will need to create your DBContext with the following override constructor:

public VegaDbContext(DbContextOptions<VegaDbContext> options) : base(options)
{
}

In your start up, you can add your context as a service like this:

services.AddDbContext<ApplicationDBContext>(config => {
    config.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
});

You can read in full detail about how dependency injection works here: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection

This part should help you with the migration. You can perform your migrations using the dotnet ef commands https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet.

When using your db context, do ensure that you are using dependency injection so you make full use of the AddDbContext function and keep it DRY.

Neville Nazerane
  • 6,622
  • 3
  • 46
  • 79
0

https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro

If I were your in your shoes, I look at this document.

Here is the simple DbContext that you can find on this webSite

namespace ContosoUniversity.Data
{
    public class SchoolContext : DbContext
    {
        public SchoolContext(DbContextOptions<SchoolContext> options) : base(options)
        {
        }

        public DbSet<Course> Courses { get; set; }
        public DbSet<Enrollment> Enrollments { get; set; }
        public DbSet<Student> Students { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Course>().ToTable("Course");
            modelBuilder.Entity<Enrollment>().ToTable("Enrollment");
            modelBuilder.Entity<Student>().ToTable("Student");
        }
    }
}
Eyup Can ARSLAN
  • 692
  • 9
  • 25
0

I just got the same error. If you are careful the error description is actually giving you the solution of the problem.   DesignTimeFactoryObject's constructor function should not take parameters.

public class ExampleDesignTimeFactory : IDesignTimeDbContextFactory<YourDBContext>{
public ExampleDesignTimeFactory(){ no constructor or no parameter constructor }
}
azorlua
  • 1
  • 3
0

I use ASP.NET CORE 3.1 to create the project and it solved