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

HasDefaultvalue() for interfaces/generics #17780

Closed
vflame opened this issue Sep 11, 2019 · 0 comments · Fixed by #25539
Closed

HasDefaultvalue() for interfaces/generics #17780

vflame opened this issue Sep 11, 2019 · 0 comments · Fixed by #25539
Labels
area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported good first issue This issue should be relatively straightforward to fix. type-bug
Milestone

Comments

@vflame
Copy link

vflame commented 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

@divega divega added this to the Backlog milestone Sep 16, 2019
@divega divega added customer-reported good first issue This issue should be relatively straightforward to fix. labels Sep 16, 2019
TobiasWolters added a commit to TobiasWolters/EntityFrameworkCore that referenced this issue Oct 5, 2019
@ajcvickers ajcvickers modified the milestones: Backlog, 6.0.0 Nov 5, 2020
@ajcvickers ajcvickers self-assigned this Nov 6, 2020
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Aug 16, 2021
ajcvickers added a commit that referenced this issue Aug 17, 2021
* Allow HasDefaultValue where value is assignable to property

Fixes #17780

* Updated baseline after merge conflict
@ajcvickers ajcvickers modified the milestones: 6.0.0, 6.0.0-rc1 Aug 18, 2021
@ajcvickers ajcvickers modified the milestones: 6.0.0-rc1, 6.0.0 Nov 8, 2021
@ajcvickers ajcvickers removed their assignment Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported good first issue This issue should be relatively straightforward to fix. type-bug
Projects
None yet
4 participants