dotnet / dotnet/Scaffolding

Scaffolding fails with a Nullable<char> in model class and assigning null

Open
#1,348 1 comment 0 reactions 0 assignees View on GitHub
area-scaffolding
Dominant language
C#
Stars
818
Forks
260
Avg merge
1d 8h
Merged PRs (30d)
10

Description

```There was an error creating a DbContext: ...\Controllers\MembersController.cs(478,12): error CS0037: Cannot convert null to 'char' because it is a non-nullable value type```

## Steps to reproduce:
Code causing the issue:

```
member.CurrentCategory = latestResult.div switch
{
5 => 'A',
10 => 'B',
20 => 'C',
30 => 'D',
_ => null
};
```

In the model class, I have the following property:
```
[Table("member")]
public partial class Member
{
/* rest of class */

public Nullable CurrentCategory { get; set; }
}
```

Scaffolding used to work fine, so I suspect this might be an update to Microsoft.VisualStudio.Web.CodeGeneration.Design (or related packages) to 3.1.3.

Changing `_ => null` to `_ => 'U'`, for example, allows scaffolding to work again.

## Expected behavior:
Scaffolding should succeed, in this case, was trying to scaffold a view.

## Actual behavior:
Scaffolding fails due to assigning null to a Nullable field.

## Additional information about the project being scaffolded, such as:
### Target framework(s):
.NetCore 3.1/AspNet.Core 3.1

### Package version of Microsoft.AspNetCore.App or Microsoft.AspNetCore.All (if applicable):
3.1.2

### Package version of Microsoft.VisualStudio.Web.CodeGeneration.Design - this may be added to your project by scaffolding:
3.1.3

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.