Skip to content

Write a DiagnosticSuppressor for CS8618 for DbSet properties #21608

Description

Activity

  1. Youssef1313 commented on Jul 13, 2020

    @Youssef1313
    MemberAuthor

    If you wish, I may try to give a PR that implements it.

  2. ajcvickers commented on Jul 13, 2020

    @ajcvickers
    Contributor

    /cc @roji

  3. roji commented on Jul 14, 2020

    @roji
    Member

    Makes sense to me... We already have Microsoft.EntityFrameworkCore.Analyzers, this could go in there. Please feel free to submit a PR for this.

  4. davidroth commented on Jul 16, 2020

    @davidroth
    Contributor

    IMO it would also make sense to document how simple expression bodied db-sets are an alternative way to define DbSet properties.

    So instead of using mutable auto properties like this:

    public class NullableReferenceTypesContext : DbContext
    {
        public DbSet<Customer> Customers { get; set; } = null!;
        public DbSet<Order> Orders { get; set; } = null!;
    }

    Using expression bodied get-only properties can be a more natural way to define them.

    public class NullableReferenceTypesContext : DbContext
    {
        public DbSet<Customer> Customers => Set<Customer>();
        public DbSet<Order> Orders => Set<Order>();
    }

    Pros:

    • Immutable DBSet properties. It makes no sense to have mutable db-sets. So why even declare them mutable if they are not.
    • "Natural" fix for CS8618 because Set<>() has a none-nullable return value.
    • No property initialization cost because when constructing a new context EF does not have to initialize all DbSet properties in advance. (Although this is highly optimized anyway, so no real measurable benefit).

    Note that I am not arguing against the suppressor. I am just wondering why the auto-properties "way" of defining DBSets, is still the default one described in all the docs etc? Probably just because of historical reasons? Does it make sense to re-evaluate this default? Thoughts @ajcvickers @roji ?

  5. roji commented on Jul 16, 2020

    @roji
    Member

    Hmm, I think this might require a bit of change in EF Core, i.e. when the DbContext constructor is doing its thing. I suspect that will especially be necessary to support read-only properties here.

    However, assuming the required changes aren't huge and it yields the same perf, I like it a lot - it definitely looks much better than the current guidance with null - and anything not requiring a suppressor is obviously better.

    @ajcvickers thoughts?

  6. ajcvickers commented on Jul 16, 2020

    @ajcvickers
    Contributor

    @roji What exactly is the question? Is the suggestion to change the guidance for if you're using NRT, or to change what we reverse engineer when NRTs are being used? Or something else?

  7. added a commit that references this issue on Jul 18, 2020
  8. roji commented on Jul 18, 2020

    @roji
    Member

    Yeah - @davidroth's suggestion above is clean, resolves the NRT warning and even makes the DbSet properties read-only, which is a bit more correct. I've submitted dotnet/EntityFramework.Docs#2520 to integrate this into our NRT guidance.

  9. changed the title [-]Write a DiagnosticSuppressor for CS8618[/-] [+]Write a DiagnosticSuppressor for CS8618 for DbSet properties[/+] on Jul 18, 2020
  10. added a commit that references this issue on Jul 18, 2020
  11. 10 remaining items

  12. added this to the 7.0.0 milestone on Oct 25, 2021
  13. added 3 commits that reference this issue on Nov 22, 2021
    8550edf
    bcffe52
    a0b2f87
  14. added a commit that references this issue on Nov 26, 2021
    e41f912
  15. modified the milestones: 7.0.0, on Feb 14, 2022
  16. modified the milestones: , 7.0.0 on Nov 5, 2022
  17. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions