You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When adding a required string property to an entity, the corresponding migration will generate a default value of "" (empty string). When scripting the DbContext from scratch using Script-DbContext, no default value is generated.
modelBuilder.Entity("Customer",e =>{ e.Property<int>("Id").ValueGeneratedOnAdd();// Generates a migration with a default value of "" specified.// When scripting the context from scratch, no default value is created. e.Property<string>("Foo").IsRequired(); e.ToTable("Customers");});
Add-Migration and subsequent Script-Migration will generate an SQL like the following (note the DEFAULT N''):
CREATE TABLE [Customers] (
[Id] intNOT NULL IDENTITY,
CONSTRAINT [PK_Customers] PRIMARY KEY ([Id])
);
GO
...
ALTERTABLE [Customers] ADD [Foo] nvarchar(max) NOT NULL DEFAULT N'';
GO
Script-DbContext will generate something like the following (no default value for Foo):
CREATE TABLE [Customers] (
[Id] intNOT NULL IDENTITY,
[Foo] nvarchar(max) NOT NULL,
CONSTRAINT [PK_Customers] PRIMARY KEY ([Id])
);
GO
Note for team: this is currently by-design. We generate the default value for the Alter to account for existing columns. Script-DbContext is creating a new database, so it does not need to handle existing rows.
Thank you for investigating this issue! I am convinced that the database generated from the migrations should align with a newly created database (via Script-DbContext). If not, it would require us to manage discrepancies during the development process, particularly in testing, which significantly increases complexity.
Regarding default values, I don’t see their value in migrations. If default values are not explicitly set by the user, then it’s likely that no appropriate default exists. This is especially true for foreign keys, for which default values are currently generated as well. Automatically setting defaults could even obscure issues that would otherwise be detected.
When adding a required string property to an entity, the corresponding migration will generate a default value of
""
(empty string). When scripting the DbContext from scratch usingScript-DbContext
, no default value is generated.E.g. migrate from
to
Add-Migration
and subsequentScript-Migration
will generate an SQL like the following (note theDEFAULT N''
):Script-DbContext
will generate something like the following (no default value for Foo):Seems to be related to #33038.
[Edit] Fixed reproducing example.
The text was updated successfully, but these errors were encountered: