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

Made it possible to use IEnumerable.Empty() in HasDefaultValue. #18240

Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
Expand Down Expand Up @@ -334,7 +335,14 @@ private static object ConvertDefaultValue([NotNull] IProperty property, [CanBeNu
{
try
{
return Convert.ChangeType(value, property.ClrType, CultureInfo.InvariantCulture);
if (typeof(IEnumerable).IsAssignableFrom(property.ClrType) && property.ClrType.IsGenericType)
TobiasWolters marked this conversation as resolved.
Show resolved Hide resolved
{
return value;
}
else
{
return Convert.ChangeType(value, property.ClrType, CultureInfo.InvariantCulture);
}
}
catch (Exception)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,16 @@ public void Relational_relationship_methods_have_non_generic_overloads()
.HasConstraintName("Simon");
}

[ConditionalFact]
public void Can_use_empty_IEnumerable_as_default_value()
{
var modelBuilder = CreateConventionModelBuilder();
modelBuilder
.Entity<Customer>()
.Property(c => c.Orders)
.HasDefaultValue(Enumerable.Empty<Order>());
}
Copy link
Member

@AndriySvyryd AndriySvyryd Oct 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an assert to check the default value was set


private void AssertIsGeneric(EntityTypeBuilder<Customer> _)
{
}
Expand Down