dotnet / dotnet/efcore

UDDT (User Defined Data Type) with a default does not have default applied.

Open
#30,160 10 comments 0 reactions 0 assignees View on GitHub
area-scaffolding customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

A database first project that has tables which use a UDDT with a default do NOT scaffold a default for the column as set on the type. Columns with defaults are scaffolded correctly.

This results in null values being set in the database as opposed to the default being applied.

Here's a thread where the issue is discussed in detail between myself and Erik Ejlskov Jensen: https://github.com/ErikEJ/EFCorePowerTools/issues/1670

Here is a script that will create the items for the db:
[EF-SqlScript2.txt](https://github.com/dotnet/efcore/files/10523788/EF-SqlScript2.txt)

Here is a project which demonstrates the issue. Notice that the generated code does not have a default set in the MstrContext.cs file for TABLE1 using the BOOL UDDT (the BOOL UDDT has a default of 0). The second table has a default set on the column and does generate code correctly.
[UDDT With Default.zip](https://github.com/dotnet/efcore/files/10520846/UDDT.With.Default.zip)

Command used for scaffolding (obviously, you'll need to add your own servername and database name, but you won't necessarily need to scaffold again as the context and models are already created):
dotnet ef dbcontext scaffold "Server=SERVERNAME;Database=DATABASENAME;Trusted_Connection=True; Encrypt=False" Microsoft.EntityFrameworkCore.SqlServer -c MstrContext -o Models --context-dir Contexts -f --use-database-names -t TABLE1 -t TABLE2

For the project, once pointed to the right server/database, update the connection string in the appsettings.json file, run and expand both functions (AddRowToTable1 & AddRowToTable2). Then, on each, click Try It Out and Execute. Then run the following on the database:

SELECT *
FROM TABLE1

SELECT *
FROM TABLE2

Notice that the default was applied for the second table with the default on the column but no default was applied to the first table with a default on the UDDT.

EFCore 7.0.2
VS 2022 (17.4.4)
Happens both with the CLI and EF Core PT - 2.5.1258
Microsoft SQL Server 2019 (RTM-CU18) (KB5017593) - 15.0.4261.1 (X64)

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.