I know it's an issue in the past since you can search the web and SO for so many questions about getting warning from EF Core model. Today I first use EF Core for .NET 8 and I just realized the warning is not there anymore:
public class AppDbContext : DbContext
{
public DbSet<DbAccount> Accounts { get; set; }
}
public class MyClass
{
// The line below still receives this warning:
// Warning CS8618 Non-nullable property 'Accounts' must contain a non-null value when exiting constructor.Consider declaring the property as nullable:
public DbSet<DbAccount> Accounts { get; set; }
}
Interestingly it skips for members of DbContext
and not DbSet
type itself as you can see from the example above, my own class still receive CS8618 warning.
I have tried reading the decompiled code of DbContext
and even DbSet
and couldn't find anything special. How does EF Core team do that? Can I apply it to my own classes somehow (for POCO JSON classes for example)?
public string MyProp{get;set;} = default!;
or if there are a lot of them I toggle it off with #nullable ... but I take it you're asserting to have code that doesn't do this or anything like it, nor any #nullable disable etc, but still you see no warning in the code generated by ef tools, but you do see warning in your own code? – Orangeadenull!
and#nullable false
currently but I am surprised EF Core could somehow did that without. The above example code I gave is from the same file,DbContext
derived class doesn't get any warning but my own class gets it. That's why I am curious and want to know how to do that. – Ragged