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

Update guidance on using default values #1398

Closed
ajcvickers opened this issue Mar 27, 2019 — with docs.microsoft.com · 5 comments · Fixed by #2972
Closed

Update guidance on using default values #1398

ajcvickers opened this issue Mar 27, 2019 — with docs.microsoft.com · 5 comments · Fixed by #2972

Comments

Copy link
Member

See discussion here: dotnet/efcore#15070 (comment)

  • For hand-written code document:
    • Use client side default values. This ensures the defaults are consistent in the domain model.
    • Show how store-generated values work in the "normal" case--e.g. with integers
    • Show how to use option B for when both the normal way and client-side defaults are not appropriate, also mentioning C
    • Show how to get the default value into the migration if it needs to be there

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

@Sergey-Nosov-GH
Copy link

Should setting default values in the parameter-less constructor of the underlying class also be an option?

For example:

class MyContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
}

public class Blog
{
    public Blog()
    {
        Rating = 3;
    }
    public int BlogId { get; set; }
    public string Url { get; set; }
    public int Rating { get; set; }
}

@ajcvickers
Copy link
Member Author

@Sergey-Nosov-GH Can you give some details on why this is preferable to:

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
    public int Rating { get; set; } = 3;
}

@Sergey-Nosov-GH
Copy link

Sergey-Nosov-GH commented Mar 29, 2019

@ajcvickers those are functionally equivalent, so your example would be fine. Nether is currently in the docs, and that is confusing. I came to this discussion from the following url:

https://docs.microsoft.com/en-us/ef/core/modeling/relational/default-values

As of right now, it has no mentions of defaults set on the underlying classes; and it makes the programmer wonder, if EF overrides such defaults, goes along with them, or what happens. Thus, more guidance would be helpful.

Thank you.

@ajcvickers
Copy link
Member Author

@Sergey-Nosov-GH Yes, this issue is for adding more guidance here. See the link in the issue content above for a link to a discussion on this. I thought you were commenting on this discussion, where this pattern is covered.

@Sergey-Nosov-GH
Copy link

Oh, yes. These discussions have at least two interfaces for adding comments: one on GitHub and one as the Feedback feature on the live documentation site. As such visitor comments on the docs site end up on GitHub as well.

So, basically, just adding another vote for more examples of different ways to set defaults; the docs site is currently limited to HasDefaultValue and HasDefaultValueSql methods only.

Documentation feedback, in action. LOL

@ajcvickers ajcvickers added this to the Backlog milestone Sep 2, 2019
@ajcvickers ajcvickers self-assigned this Jan 22, 2020
@ajcvickers ajcvickers modified the milestones: Backlog, 5.0.0 Dec 30, 2020
ajcvickers added a commit that referenced this issue Dec 30, 2020
Fixes #2725
Fixes #2707
Fixes #2525
Fixes #2524
Fixes #2331
Fixes #2076
Fixes #1489
Fixes #1453
Fixes #1398
Fixes #1339
Fixes #1235
Fixes #1007
Fixes #863
Fixes #786
Fixes #531
Fixes #505
ajcvickers added a commit that referenced this issue Dec 30, 2020
Fixes #2725
Fixes #2707
Fixes #2525
Fixes #2524
Fixes #2331
Fixes #2076
Fixes #1489
Fixes #1453
Fixes #1398
Fixes #1339
Fixes #1235
Fixes #1007
Fixes #863
Fixes #786
Fixes #531
Fixes #505
ajcvickers added a commit that referenced this issue Dec 31, 2020
Fixes #2725
Fixes #2707
Fixes #2525
Fixes #2524
Fixes #2331
Fixes #2105
Fixes #2076
Fixes #1541
Fixes #1489
Fixes #1453
Fixes #1398
Fixes #1339
Fixes #1235
Fixes #1007
Fixes #863
Fixes #815
Fixes #786
Fixes #531
Fixes #505
ajcvickers added a commit that referenced this issue Jan 6, 2021
Fixes #2725
Fixes #2707
Fixes #2525
Fixes #2524
Fixes #2331
Fixes #2076
Fixes #1489
Fixes #1453
Fixes #1398
Fixes #1339
Fixes #1235
Fixes #1007
Fixes #863
Fixes #786
Fixes #531
Fixes #505
ajcvickers added a commit that referenced this issue Jan 6, 2021
Fixes #2725
Fixes #2707
Fixes #2525
Fixes #2524
Fixes #2331
Fixes #2105
Fixes #2076
Fixes #1541
Fixes #1489
Fixes #1453
Fixes #1398
Fixes #1339
Fixes #1235
Fixes #1007
Fixes #863
Fixes #815
Fixes #786
Fixes #531
Fixes #505
ajcvickers added a commit that referenced this issue Jan 6, 2021
Fixes #2725
Fixes #2707
Fixes #2525
Fixes #2524
Fixes #2331
Fixes #2105
Fixes #2076
Fixes #1541
Fixes #1489
Fixes #1453
Fixes #1398
Fixes #1339
Fixes #1235
Fixes #1007
Fixes #863
Fixes #815
Fixes #786
Fixes #531
Fixes #505
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants