Closed
Description
openedon Sep 11, 2019
Unable to set default value for interfaces/generics.
Not working:
Model:
public class EventTriggers
{
public IEnumerable<Regex> Triggers { get; private set; }
}
Context Configuration:
builder.OwnsOne(t => t.EventTriggers, trigger =>
{
trigger.Property(p => p.Triggers)
.HasConversion<string>(
to => to.SerializeToJson(Newtonsoft.Json.Formatting.None, true),
from => from.DeserializeFromJson<IEnumerable<Regex>>())
.HasDefaultValue(Enumerable.Empty<Regex>());
});
Results in the following when running add-migration:
Cannot set default value 'System.Text.RegularExpressions.Regex[]' of type 'System.Text.RegularExpressions.Regex[]' on property 'Triggers' of type 'System.Collections.Generic.IEnumerable`1[System.Text.RegularExpressions.Regex]' in entity type 'EventTriggers'.
The following is working:
Model:
public class EventTriggers
{
public List<Regex> Triggers { get; private set; }
}
Context Configuration:
builder.OwnsOne(t => t.EventTriggers, trigger =>
{
trigger.Property(p => p.Triggers)
.HasConversion<string>(
to => to.SerializeToJson(Newtonsoft.Json.Formatting.None, true),
from => from.DeserializeFromJson<List<Regex>>())
.HasDefaultValue(new List<Regex>());
});
Is this by design?
EF Core version: 2.2.6
Database provider: npgsql
Target framework: (e.g. .NET Core 2.2)
Operating system: Cross Platform
IDE: VS 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment