Page 1 of 1

Issue w/ navigation properties

Posted: Wed 07 Jun 2017 20:12
by guinness
Using 6.1.265 version and I'm having a problem with generated nav props for EF Core. I generated a model from database, but due to a referential integrity issue I had to make a class field nullable.

That worked and made the field nullable in the generated code. However, the nav prop in the context class was incorrect.

GENERATED:

Code: Select all

modelBuilder.Entity<State>().HasOne(x => x.State).WithMany().HasForeignKey(@"StateId");
SHOULD BE:

Code: Select all

modelBuilder.Entity<State>().HasOne(x => x.State).WithMany().IsRequired(false).HasForeignKey(@"StateId");
Is the issue related to the default parameter value for IsRequired()?
IsRequired(bool required = true)

Re: Issue w/ navigation properties

Posted: Mon 12 Jun 2017 12:56
by Shalex
Thank you for your report. We will notify you when generation of .IsRequired(false) is added.

Re: Issue w/ navigation properties

Posted: Wed 28 Jun 2017 17:39
by Shalex
The issue is fixed in Entity Developer v6.1.284. Please try it and notify us about the result.

Re: Issue w/ navigation properties

Posted: Thu 29 Jun 2017 01:32
by guinness
It appears to be working Shalex. Below is when the FK is not nullable:

Code: Select all

modelBuilder.Entity<State>().HasMany(x => x.Cities).WithOne(op => op.State)
  .IsRequired(true).HasForeignKey(@"StateId");
modelBuilder.Entity<City>().HasOne(x => x.State).WithMany(op => op.Cities)
  .IsRequired(true).HasForeignKey(@"StateId");
When the model is changed to make the FK nullable, the following is generated:

Code: Select all

modelBuilder.Entity<State>().HasMany(x => x.Cities).WithOne(op => op.State)
  .HasForeignKey(@"StateId");
modelBuilder.Entity<City>().HasOne(x => x.State).WithMany(op => op.Cities)
  .IsRequired(false).HasForeignKey(@"StateId");
However, I'm not sure why both lines above don't have IsRequired(false) specifying that the relationship is optional.

Re: Issue w/ navigation properties

Posted: Tue 04 Jul 2017 08:04
by Shalex
Thank you for your report. We will notify you when the code generation is fixed.

Re: Issue w/ navigation properties

Posted: Fri 14 Jul 2017 11:46
by Shalex
The behaviour is changed: IsRequired(false / true) for navigation properties is always generated in EF Core fluent mapping now. Refer to viewtopic.php?f=32&t=35671.

Re: Issue w/ navigation properties

Posted: Sat 15 Jul 2017 21:08
by guinness
@Shalex confirmed working.