Issue w/ navigation properties

Discussion of open issues, suggestions and bugs regarding Entity Developer - ORM modeling and code generation tool
Post Reply
guinness
Posts: 25
Joined: Tue 22 Nov 2016 03:19

Issue w/ navigation properties

Post by guinness » Wed 07 Jun 2017 20:12

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)

Shalex
Site Admin
Posts: 8248
Joined: Thu 14 Aug 2008 12:44

Re: Issue w/ navigation properties

Post by Shalex » Mon 12 Jun 2017 12:56

Thank you for your report. We will notify you when generation of .IsRequired(false) is added.

Shalex
Site Admin
Posts: 8248
Joined: Thu 14 Aug 2008 12:44

Re: Issue w/ navigation properties

Post by Shalex » Wed 28 Jun 2017 17:39

The issue is fixed in Entity Developer v6.1.284. Please try it and notify us about the result.

guinness
Posts: 25
Joined: Tue 22 Nov 2016 03:19

Re: Issue w/ navigation properties

Post by guinness » Thu 29 Jun 2017 01:32

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.

Shalex
Site Admin
Posts: 8248
Joined: Thu 14 Aug 2008 12:44

Re: Issue w/ navigation properties

Post by Shalex » Tue 04 Jul 2017 08:04

Thank you for your report. We will notify you when the code generation is fixed.

Shalex
Site Admin
Posts: 8248
Joined: Thu 14 Aug 2008 12:44

Re: Issue w/ navigation properties

Post by Shalex » Fri 14 Jul 2017 11:46

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.

guinness
Posts: 25
Joined: Tue 22 Nov 2016 03:19

Re: Issue w/ navigation properties

Post by guinness » Sat 15 Jul 2017 21:08

@Shalex confirmed working.

Post Reply