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

Default value forced when converting an EXISTING string column to be non-nullable #31595

Closed
timker opened this issue Aug 31, 2023 · 1 comment

Comments

@timker
Copy link

timker commented Aug 31, 2023

I'm adding a non nullable string property to an existing and it forces the default value to be ''
However, if I'm creating a table from scratch (with the property), there is no default value.

There are two issue with this:

  1. .HasDefaultValue(null) doesn't work, and this is no .DisableDefaultValue()

  2. You database schema could different depending in the migration path you took.

It is also awkward to remove the default value,
I generally try to avoid modifying generated add migration code.
The only "pure cli way" i can find is to add a migration .HasDefaultValue('') and then add another removing .HasDefaultValue('')
this works.... (is there a better way?)

Include your code

using Microsoft.EntityFrameworkCore;
Console.WriteLine("Welcome");
/*
 steps
 * dotnet ef migrations add InitialCreate

 * Uncomment 
     //public string Name { get; set; } 
   in ExampleEvolution

 * dotnet ef migrations add AddRequiredNamePropertyAfterTableCreation
 
Notice:
       Observe ExampleTable & ExampleEvolution are the same
       ExampleTable.Name has no default value
       ExampleEvolution.Name has a default Value: ''

 */



public class BloggingContext : DbContext
{

    public DbSet<ExampleTable> ExampleTable { get; set; }

    public DbSet<ExampleEvolution> ExampleEvolution { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder options)  => options.UseSqlServer($"");
}

public class ExampleTable
{
    public int Id { get; set; }
    public string Name { get; set; }
}


public class ExampleEvolution
{
    public int Id { get; set; }

    // uncomment for second migration
    // public string Name { get; set; }
}
@bricelam
Copy link
Contributor

Duplicate of #25899

@bricelam bricelam marked this as a duplicate of #25899 Aug 31, 2023
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Sep 5, 2023
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