Skip to content

HasDefaultvalue() for interfaces/generics #17780

Closed

Description

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

Metadata

Assignees

No one assigned

    Labels

    area-model-buildingclosed-fixedThe issue has been fixed and is/will be included in the release indicated by the issue milestone.customer-reportedgood first issueThis issue should be relatively straightforward to fix.type-bug

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions