Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration with dependency on constructor #6574

Closed
vquaiato opened this issue Sep 19, 2016 · 2 comments
Closed

Migration with dependency on constructor #6574

vquaiato opened this issue Sep 19, 2016 · 2 comments

Comments

@vquaiato
Copy link

Hello. I am migrating from RC1 to RTM.

Steps to reproduce

With RC1 I had a migration with a dependency on its constructor:

public partial class MigrationWithDependency : Migration
{
    private readonly IDependency<Foo> theDependency;

    public MigrationWithDependency ( IDependency<Foo> theDependency)
    {
        this.theDependency= theDependency;
    }

    protected override void Up(MigrationBuilder migrationBuilder)
    {
        var something = this.theDependency.DoSomething();
        migrationBuilder.Sql($"UPDATE Foo SET Bar = '{something}'");
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        // irrelevant
    }
}

The issue

When I try to run something like database update I am getting the following error pointing out that I don't have a parameterless constructor for this object:

~/ dotnet ef database update -c myContext
Project FooWebApp (.NETFramework,Version=v4.5.2) was previously compiled. Skipping compilation.
System.MissingMethodException: No parameterless constructor defined for this object.
   em System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   em System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   em System.Activator.CreateInstance(Type type, Boolean nonPublic)
   em System.Activator.CreateInstance(Type type)
   em Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsAssembly.CreateMigration(TypeInfo migrationClass, String activeProvider)
   em Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.<PopulateMigrations>b__14_4(KeyValuePair`2 p)
   em System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   em System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   em System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   em Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.PopulateMigrations(IEnumerable`1 appliedMigrationEntries, String targetMigration, IReadOnlyList`1& migrationsToApply, IReadOnlyList`1& migrationsToRevert)
   em Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.<GetMigrationCommandLists>d__13.MoveNext()
   em Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   em Microsoft.EntityFrameworkCore.Design.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
   em Microsoft.EntityFrameworkCore.Tools.Cli.DatabaseUpdateCommand.<>c__DisplayClass0_0.<Configure>b__0()
   em Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
   em Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args)
No parameterless constructor defined for this object.

Previously in the RC1 dependencies were loaded from Startup.cs. Was it changed now? How can I pass dependencies to my migration constructors?

Also It would be valuable to have the class name in question exposed on the exception message... Among more than 50 migrations it took a lot to figure out what was the problem.

Further technical details

".NETFramework,Version=v4.5.2"
"Microsoft.AspNetCore.Mvc": "1.0.0"
"Microsoft.EntityFrameworkCore": "1.0.0"

It is probably similar to what you did with contexts: #5312

@rowanmiller
Copy link
Contributor

Hey,

We'd like some more info on the scenario you are trying to achieve here to help us better triage the request. This may be quite difficult for us to do as EF doesn't have access to the application service provider where you are presumably registering the service you want to inject, but we'll consider it if there is a compelling scenario.

~Rowan

@rowanmiller
Copy link
Contributor

rowanmiller commented Oct 3, 2016

EF Team Triage: Closing this issue as the requested additional details have not been provided.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants