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

Write a DiagnosticSuppressor for CS8618 for DbSet properties #21608

Closed
Youssef1313 opened this issue Jul 13, 2020 · 9 comments · Fixed by #26795
Closed

Write a DiagnosticSuppressor for CS8618 for DbSet properties #21608

Youssef1313 opened this issue Jul 13, 2020 · 9 comments · Fixed by #26795
Assignees
Labels
area-analyzer closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Milestone

Comments

@Youssef1313
Copy link
Member

See dotnet/roslyn#45912

@Youssef1313
Copy link
Member Author

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

@ajcvickers
Copy link
Member

/cc @roji

@roji
Copy link
Member

roji commented Jul 14, 2020

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

@davidroth
Copy link
Contributor

davidroth commented Jul 16, 2020

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 ?

@roji
Copy link
Member

roji commented Jul 16, 2020

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?

@ajcvickers
Copy link
Member

@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?

@roji
Copy link
Member

roji commented Jul 18, 2020

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.

@roji roji closed this as completed Jul 18, 2020
@roji roji changed the title Write a DiagnosticSuppressor for CS8618 Write a DiagnosticSuppressor for CS8618 for DbSet properties Jul 18, 2020
roji added a commit to dotnet/EntityFramework.Docs that referenced this issue Jul 18, 2020
roji added a commit to dotnet/EntityFramework.Docs that referenced this issue Jul 20, 2020
@Neme12
Copy link

Neme12 commented Jul 14, 2021

@roji I still think the diagnostic suppressor would be a good addition because defining DbSets as auto-properties is still supported.

@roji roji reopened this Jul 16, 2021
@roji
Copy link
Member

roji commented Jul 16, 2021

As suggested, reopening to consider doing this so that the following doesn't generate an NRT warning:

public class SomeContextContext : DbContext
{
    public DbSet<Customer> Customers { get; set; }
}

roji added a commit to roji/efcore that referenced this issue Nov 22, 2021
roji added a commit to roji/efcore that referenced this issue Nov 22, 2021
roji added a commit to roji/efcore that referenced this issue Nov 22, 2021
@roji roji added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Nov 22, 2021
@ajcvickers ajcvickers modified the milestones: 7.0.0, 7.0.0-preview1 Feb 14, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0-preview1, 7.0.0 Nov 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-analyzer closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Projects
None yet
5 participants