How to make Entity Framework Core create a foreign key constraint that is not enforced

1.2k views Asked by At

I'm using code-first with EF Core 3.1.2

I have established the following two-way one-to-one relationship in the OnModelCreating method of my DbContext.

modelBuilder.Entity<Inspection>()
    .HasOne<Defect>(i => i.SpecialDefect)
    .WithOne(d => d.SpecialInspection);

This creates foreign key constraints in the database. I would like these constrains to be non-enforced. i.e. I would like the "Enforce Foreign Key Constraint" option to be set to "No". By default, this is set to yes.

enter image description here

Is this possible? If so, how might I do this?

1

There are 1 answers

0
AudioBubble On

If you need to add a row with a NULL value in column "DefectId", you can easily define that as a NULLABLE in the corresponding class.

public int? DefectId {get;set;}