Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrations: Inconsistency between migration and Script-DbContext when adding a required string property. #33604

Closed
minichma opened this issue Apr 24, 2024 · 3 comments

Comments

@minichma
Copy link

minichma commented Apr 24, 2024

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.

E.g. migrate from

        modelBuilder.Entity(
              "Customer", e =>
              {
                  e.Property<int>("Id").ValueGeneratedOnAdd();
                  e.ToTable("Customers");
              });

to

        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] int NOT NULL IDENTITY,
    CONSTRAINT [PK_Customers] PRIMARY KEY ([Id])
);
GO

...

ALTER TABLE [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] int NOT NULL IDENTITY,
    [Foo] nvarchar(max) NOT NULL,
    CONSTRAINT [PK_Customers] PRIMARY KEY ([Id])
);
GO

Seems to be related to #33038.

[Edit] Fixed reproducing example.

@ajcvickers
Copy link
Member

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.

@minichma
Copy link
Author

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.

@ajcvickers
Copy link
Member

@minichma This is tracked by #27084.

@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants