UDDT (User Defined Data Type) with a default does not have default applied.
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
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
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. The 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)
Is your script not wrong? Both _FLG columns are NOT NULL ...
Ha! I'd have swore I re-saved those. Ah, saved to a different directory. Corrected.
Must be Friday.
@ajcvickers, it looks like you closed this issue with no information? I'm a little lost on why this wouldn't constitute a bug or have some sort of follow-up for EF?
Any information is appreciated.
@ChuckMusgrave Sorry, I thought the behavior was due to the script error that Erik pointed out. I didn't look too closely; I will take another look.
Thank you very much.
To be clear, it's a bug where the default isn't being generated on a column that contains a UDDT that has a default.
Note for triage: related to #12796.
Note from triage: putting this on the backlog to add support for:
CREATE TYPE [dbo].[BOOL] FROM [tinyint] NOT NULL
GO
CREATE DEFAULT [dbo].[BOOL]
AS
0
GO
Thank you @ajcvickers, any thoughts on a possible release time frame?
@ChuckMusgrave I doubt it will happen anytime soon. There are a lot of other higher-priority features on the backlog.
Bummer, but...thank you :)