So EF Core preview 7 was released and I decided to use it along with C# 8 previews and .NET Core 3.0 preview 7. Let's say I have a class representing a many-to-many relationship:
public class A
{
public int Id { get; set; }
public ICollection<Relation> Relations { get; set; }
}
public class B
{
public int Id { get; set; }
public ICollection<Relation> Relations { get; set; }
}
public class Relation
{
public A A { get; set; }
public B B { get; set; }
public int AId { get; set; }
public int BId { get; set; }
}
I'd map them as this:
modelBuilder.Entity<A>(entity => entity.HasKey(e => e.Id));
modelBuilder.Entity<B>(entity => entity.HasKey(e => e.Id));
modelBuilder.Entity<Relation>(entity =>
{
entity.HasKey(e => new { e.AId, e.BId });
entity.HasOne(e => e.A).WithMany(a => a.Relations).HasForeignKey(e => e.AId);
entity.HasOne(e => e.B).WithMany(b => b.Relations).HasForeignKey(e => e.BId);
});
Now, since I might not want to include one or both of the relation's classes, A
and B
can be null. Thus, they should be nullable.
var relation = Set<Relations>().Include(r => r.A).First(); // relation.A is not null, but relation.B is null.
So I'd rewrite the class as:
public class Relation
{
public A? A { get; set; }
public B? B { get; set; }
}
But now the model building won't work, because these lines:
entity.HasOne(e => e.A).WithMany(a => a.Relations).HasForeignKey(e => e.AId);
entity.HasOne(e => e.B).WithMany(b => b.Relations).HasForeignKey(e => e.BId);
raise CS8602 - Dereference of a possibly null reference
on the a.Relations
nad b.Relations
access, which I set to be treated as errors solution-wide, because it seemed like a sane thing to do.
Note that model building from the other side, so configuring HasMany
on A
and B
, will raise CS8603 - Possible null reference return
.
I was able to silently work around the issue by going #pragma warning disable CS8602
but it's obviously a work-around. It looks to me like a smell in EF Core, it'd appear reasonable for this usage to be correct and never raise any issues with null
. I was unable to find such an issue on EF Core's github, however.
So the question is, is there a way to have nullable navigational properties without raising a warning on model building in the current EF Core 3.0.0 Preview 7? If not, and this is indeed an issue, is it known and I missed it on EF Core's github or should I raise it there?
#pragma warning disabling
all of these :( – Avalokitesvara#nullable disable
beforeOnModelCreating
and#nullable restore
after. – Avalokitesvara