What is the replacement for hasRequired with EF core?

user8520658 picture user8520658 · May 29, 2018 · Viewed 10.8k times · Source

With Entity Framework we had HasRequired on a field. What is the alternative replacement for that with EF core?

https://msdn.microsoft.com/en-us/library/jj591620(v=vs.113).aspx

I tried with HasRequired but it throws error.

Answer

Prescott picture Prescott · May 29, 2018

Check out the Required and Optional Relationships in the EF Core Documentation.

Specifically I think you'll want something like:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<MyEntity>()
        .HasOne(p => p.Relationship)
        .IsRequired();
}

Or something like that - you haven't given much information to go on