Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,4 @@ ASALocalRun/

# Local History for Visual Studio
.localhistory/
full.targets.txt
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,16 @@ public override IReadOnlyList<MethodCallCodeFragment> GenerateFluentApiCalls(
defaultValueAnnotation.Value,
defaultConstraintNameAnnotation.Value));
}
else
else if (defaultValueSqlAnnotation != null)
{
Check.DebugAssert(
defaultValueSqlAnnotation != null,
$"Default constraint name was set for {property.Name}, but DefaultValue and DefaultValueSql are both null.");
fragments.Add(
new MethodCallCodeFragment(
nameof(SqlServerPropertyBuilderExtensions.HasDefaultValueSql),
defaultValueSqlAnnotation.Value,
defaultConstraintNameAnnotation.Value));
}
// If neither DefaultValue nor DefaultValueSql annotation exists (e.g., they were already removed
// because the default value equals the CLR default), skip generating code for the constraint name
}

var isPrimitiveCollection = property.IsPrimitiveCollection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,33 @@ public void GenerateFluentApi_IProperty_works_with_IsSparse()
}
}

[ConditionalFact]
public void GenerateFluentApi_IProperty_with_DefaultConstraintName_and_no_DefaultValue_does_not_throw()
{
// Reproduces https://github.com/dotnet/efcore/issues/37175
// When a property has a named default constraint but no DefaultValue or DefaultValueSql annotation,
// GenerateFluentApiCalls should not throw a NullReferenceException
var generator = CreateGenerator();
var modelBuilder = SqlServerConventionSetBuilder.CreateModelBuilder();
modelBuilder.Entity("Post", x => x.Property<int>("Id"));
var property = modelBuilder.Model.FindEntityType("Post")!.FindProperty("Id")!;

// Create annotations dictionary with only DefaultConstraintName (simulating the scenario where
// DefaultValue was already removed but DefaultConstraintName wasn't)
var constraintNameAnnotation = RelationalAnnotationNames.DefaultConstraintName;
var annotations = new Dictionary<string, IAnnotation>
{
{ constraintNameAnnotation, new Annotation(constraintNameAnnotation, "DF_Post_Id") }
};

// This should not throw - it should simply skip generating code for the constraint name
// since there's no default value to associate with it
var result = generator.GenerateFluentApiCalls((IProperty)property, annotations);

// No method calls should be generated for the constraint name alone
Assert.Empty(result);
}

[ConditionalFact]
public void GenerateFluentApi_IModel_works_with_DatabaseMaxSize()
{
Expand Down