0

I have 2 models as follows.

Box

DocketDetail

A DocketDetail can have upto 3 boxes associated with it (the first one being mandatory, the second two are optional). The DocketDetail model has this relationship defined as follows.

 [ForeignKey("Box1Id")]
    public Box Box1 { get; set; }           
    public Guid Box1Id { get; set;}

    [ForeignKey("Box2Id")]
    public Box Box2 { get; set; }           
    public Guid? Box2Id { get; set;}
    
    [ForeignKey("Box3Id")]        
    public Box Box3 { get; set; }                   
    public Guid? Box3Id { get; set;}

I also have the following Fluent API script to set the relationship as optional

 modelBuilder.Entity<DocketDetail>()
            .HasOne(dd => dd.Box1)
            .WithMany()
            .OnDelete(DeleteBehavior.Restrict);

        modelBuilder.Entity<DocketDetail>()
            .HasOne(dd => dd.Box2)
            .WithMany()
            .HasForeignKey(dd => dd.Box2Id) 
            .IsRequired(false)
            .OnDelete(DeleteBehavior.NoAction);

        modelBuilder.Entity<DocketDetail>()
            .HasOne(dd => dd.Box3)
            .WithMany()     
            .HasForeignKey(dd => dd.Box3Id) 
            .IsRequired(false)
            .OnDelete(DeleteBehavior.NoAction);

However, the EF migration generates the foreign keys as mandatory. What am I missing to make these relationships optional?

 migrationBuilder.AddForeignKey(
            name: "FK_DocketDetail_Box_Box1Id",
            table: "DocketDetail",
            column: "Box1Id",
            principalTable: "Box",
            principalColumn: "Id",
            onDelete: ReferentialAction.Restrict);

        migrationBuilder.AddForeignKey(
            name: "FK_DocketDetail_Box_Box2Id",
            table: "DocketDetail",
            column: "Box2Id",
            principalTable: "Box",
            principalColumn: "Id");

        migrationBuilder.AddForeignKey(
            name: "FK_DocketDetail_Box_Box3Id",
            table: "DocketDetail",
            column: "Box3Id",
            principalTable: "Box",
            principalColumn: "Id");
jps
  • 20,041
  • 15
  • 75
  • 79
OEDev
  • 98
  • 8
  • Make use of .HasOptional, i think your query has been answered here: https://stackoverflow.com/questions/8019999/how-do-i-define-foreign-key-optional-relationships-in-fluentapi-data-annotations – Dave Morrison Sep 21 '21 at 10:33
  • Thanks but HasOptional is no longer present in EF Core https://stackoverflow.com/questions/35562483/equivalent-for-hasoptional-in-entity-framework-core-1-ef7 By setting the property as Nullable it should make it optional according to that post, but as my ID values are Guid's then these can't be null, which may be my issue? – OEDev Sep 21 '21 at 10:37

1 Answers1

0

So the issue was not the model or the Fluent API. I was trying to store a Empty Guid (System.Guid.Empty) in the optional box fields, but that generated an foreign key constraint error. Storing null (as the field is nullable) resolves the constraint error.

OEDev
  • 98
  • 8