0

I have a database table which is referenced in different tables. The relation is a one to one.

Assuming I have three database tables:

Detail with one field DetailId
Foo with the fields FooId and DetailId
Bar with the field BarId and DetailId

My models would look like this

public class Detail
{
    public int DetailId { get; set; }
}

public class Foo
{
    public int FooId { get; set; }
    [Required]
    public int DetailId { get; set; }
    [Required]
    public Detail Detail { get; set; }
}

public class Bar
{
    public int BarId { get; set; }
    [Required]
    public int DetailId { get; set; }
    [Required]
    public Detail Detail { get; set; }
}

And in my Configuration I have

public class BarConfiguration : IEntityTypeConfiguration<Bar>
{
    public void Configure(EntityTypeBuilder<Bar> builder)
    {
        builder.HasOne(b => b.Detail)
            .WithOne()
            .HasForeignKey<Bar>(x => x.DetailId);
    }
}

My problem is: When I load an object of Bar my DetailId is set to the correct value but my Detail property is null.

My BarConfiguration is called in my context in protected override void OnModelCreating(ModelBuilder modelBuilder).

Mighty Badaboom
  • 6,067
  • 5
  • 34
  • 51

0 Answers0