20

I'm trying to create an optional foreign key using Entity Framework 7 and the Fluid-API. In EF v6.x we had the option to add this using .WithOptional or .HasOptional, but I cant find any equivalent functionality in EF 7.. any ideas?

Br, Inx

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Inx51
  • 1,911
  • 3
  • 24
  • 44

1 Answers1

37

Found the answer.. you can pass in "false" as a parameter to .IsRequired().. For instance:

            EntityShortcut<ContentEntity>()
            .HasMany(e => e.Children)
            .WithOne(e => e.Parent)
            .IsRequired();

That would be an requried relation

            EntityShortcut<ContentEntity>()
            .HasMany(e => e.Children)
            .WithOne(e => e.Parent)
            .IsRequired(false)

While that would NOT be a required relation.

FYI:

private static EntityTypeBuilder<T> EntityShortcut<T>() where T : class
{
    return _modelBuilder.Entity<T>();
}
Inx51
  • 1,911
  • 3
  • 24
  • 44