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

nullreference when during add-migration when migration implementations lack migration attribute #9920

Closed
kbilsted opened this issue Sep 28, 2017 · 1 comment
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. good first issue This issue should be relatively straightforward to fix. help wanted This issue involves technologies where we are not experts. Expert help would be appreciated. type-bug
Milestone

Comments

@kbilsted
Copy link

kbilsted commented Sep 28, 2017

if you implement a migration hook and forgetting the attribute. Then call add-migration foo

If you are seeing an exception, include the full exceptions details (message and stack trace).

System.ArgumentNullException: Value cannot be null.
Parameter name: key
   at System.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
   at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
   at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsAssembly.<>c__DisplayClass3_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Internal.LazyRef`1.get_Value()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsAssembly.get_Migrations()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsAssembly.FindMigrationId(String nameOrId)
   at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)

Steps to reproduce

    [DbContext(typeof(CustomerContext))]
    //[Migration("Custom migrations")]
    class MigrationHook : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
        }
    }
}

Further technical details

The problem is the below code from MigrationsAssembly.cs using ?. not realizing that you cannot use null as a key. The error message should not be nullreferenceexception, but rather that the implementor forgot to apply an attribute.

Similarly, if using the attribute, and forgetting the subclassing, and error should be show. This is not the case now where only subclasses are investigated

https://github.com/aspnet/EntityFrameworkCore/blob/888b188731264cf2cebee16ce5a9ad4e65fe6f8c/src/EFCore.Relational/Migrations/Internal/MigrationsAssembly.cs

_migrations = new LazyRef<IReadOnlyDictionary<string, TypeInfo>>(
            () => (
                    from t in Assembly.GetConstructableTypes()
                    where t.IsSubclassOf(typeof(Migration))
                          && t.GetCustomAttribute<DbContextAttribute>()?.ContextType == contextType
                    let id = t.GetCustomAttribute<MigrationAttribute>()?.Id
                    orderby id
                    select new { Key = id, Element = t })
                .ToDictionary(i => i.Key, i => i.Element));
@ajcvickers ajcvickers added this to the 2.1.0 milestone Oct 4, 2017
@ajcvickers ajcvickers added help wanted This issue involves technologies where we are not experts. Expert help would be appreciated. type-bug labels Oct 4, 2017
@ajcvickers ajcvickers modified the milestones: 2.1.0-preview1, 2.1.0 Jan 17, 2018
@bricelam
Copy link
Contributor

If using the attribute, and forgetting the subclassing, an error should be show.

Having [Migration] without inheriting from Migration seems very unlikely. Classes like these will continue to be silently ignored for now.

@bricelam bricelam added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Feb 16, 2018
@bricelam bricelam added the good first issue This issue should be relatively straightforward to fix. label May 31, 2019
@ajcvickers ajcvickers modified the milestones: 2.1.0-preview2, 2.1.0 Nov 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. good first issue This issue should be relatively straightforward to fix. help wanted This issue involves technologies where we are not experts. Expert help would be appreciated. type-bug
Projects
None yet
Development

No branches or pull requests

3 participants