Search code examples
c#visual-studiodefault-valuescaffolding

New language feature breaks scaffolding - Could not get the reflection type for DbContext


So new language features implements the option to shorthand the default keyword.

public void GenericMethod<T>(T responseObject = default)
{
    //Your code here
}

Instead of the old

public void GenericMethod<T>(T responseObject = default(T))
{
    //Your code here
}

When using the new shorthand we experienced a bug where one can't scaffold a view or controller in dotnet core. - Giving the Error "There was an error running the selected code generator: Could not get the reflection type for DbContext : ...."

By reverting back to the old way of implemented the "default" the error goes away.

As of this momement the new way of using the default keyword is the only one we have experienced breaking it but I assume there might be problems with similar new language features.

This isn't really a question. We just thought it would be nice to spread the word of this bug; As no post covered this issue.

Visual studio : Visual studio 2019

Target framework : .NET Core 2.2

EDIT: This is not a problem with EF. If one tries to scaffold a View without the dbContext where you use a Model class(FooClass) the error changes to: Could not get the reflection type for Model: FooClass


Solution

  • Similar Issue here. Changing it back to the old way worked.

    public static T Get<T>(this ISession session, string key)
            {
                var value = session.GetString(key);
    
                return value == null ? default(T) : JsonConvert.DeserializeObject<T>(value);
            }
    
    public static T Get<T>(this ISession session, string key)
            {
                var value = session.GetString(key);
    
                return value == null ? default : JsonConvert.DeserializeObject<T>(value);
            }