No parameterless constructor defined for this object in asp.netcore migrations

北城以北 提交于 2020-01-06 06:19:06

问题


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);
}

回答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://docs.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://docs.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.




回答2:


https://docs.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");
        }
    }
}



回答3:


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 }
}


来源:https://stackoverflow.com/questions/48854417/no-parameterless-constructor-defined-for-this-object-in-asp-netcore-migrations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!