-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Comments
If you wish, I may try to give a PR that implements it. |
/cc @roji |
Makes sense to me... We already have Microsoft.EntityFrameworkCore.Analyzers, this could go in there. Please feel free to submit a PR for this. |
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:
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 ? |
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? |
@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? |
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 I still think the diagnostic suppressor would be a good addition because defining |
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; }
} |
See dotnet/roslyn#45912
The text was updated successfully, but these errors were encountered: