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

Allow sharing column when properties have different nullability. #25740

Merged
merged 1 commit into from
Aug 27, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -901,9 +901,11 @@ protected virtual void ValidateCompatible(
currentStored));
}

var currentDefaultValue = property.GetDefaultValue(storeObject);
var previousDefaultValue = duplicateProperty.GetDefaultValue(storeObject);
if (!Equals(currentDefaultValue, previousDefaultValue))
var hasDefaultValue = property.TryGetDefaultValue(storeObject, out var currentDefaultValue);
var duplicateHasDefaultValue = duplicateProperty.TryGetDefaultValue(storeObject, out var previousDefaultValue);
if ((hasDefaultValue
|| duplicateHasDefaultValue)
&& !Equals(currentDefaultValue, previousDefaultValue))
{
currentDefaultValue = GetDefaultColumnValue(property, storeObject);
previousDefaultValue = GetDefaultColumnValue(duplicateProperty, storeObject);
Expand Down
25 changes: 15 additions & 10 deletions src/EFCore.Relational/Metadata/IColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,26 @@ public virtual object? DefaultValue
/// <returns> True if the default value was explicitly set; false otherwise. </returns>
public virtual bool TryGetDefaultValue(out object? defaultValue)
{
var property = PropertyMappings.First().Property;

if (!property.TryGetDefaultValue(StoreObjectIdentifier.Table(Table.Name, Table.Schema), out defaultValue))
foreach (var mapping in PropertyMappings)
{
return false;
}
var property = mapping.Property;
if (!property.TryGetDefaultValue(StoreObjectIdentifier.Table(Table.Name, Table.Schema), out defaultValue))
{
continue;
}

var converter = property.GetValueConverter() ?? PropertyMappings.First().TypeMapping.Converter;
var converter = property.GetValueConverter() ?? PropertyMappings.First().TypeMapping.Converter;

if (converter != null)
{
defaultValue = converter.ConvertToProvider(defaultValue);
if (converter != null)
{
defaultValue = converter.ConvertToProvider(defaultValue);
}

return true;
}

return true;
defaultValue = null;
return false;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,23 @@ public virtual void Detects_duplicate_column_names_within_hierarchy_with_differe
modelBuilder);
}

[ConditionalFact]
public virtual void Passes_on_duplicate_column_names_within_hierarchy_with_same_column_nullability()
{
var modelBuilder = CreateConventionalModelBuilder();
modelBuilder.Entity<Animal>();
modelBuilder.Entity<Cat>().Property<int>("OtherId").HasColumnName("OtherId");
modelBuilder.Entity<Dog>().Property<int?>("OtherId").HasColumnName("OtherId");

var model = Validate(modelBuilder);

var column = model.FindEntityType(typeof(Cat)).FindProperty("OtherId").GetTableColumnMappings().Single().Column;

Assert.Equal(2, column.PropertyMappings.Count());
Assert.True(column.IsNullable);
Assert.Null(column.DefaultValue);
}

[ConditionalFact]
public virtual void Detects_duplicate_column_names_within_hierarchy_with_different_comments()
{
Expand Down