msawczyn / EFDesigner2022

Entity Framework visual design surface and code-first code generation for EF6, Core and beyond
MIT License
119 stars 21 forks source link

Setting custom foreign key names not working as expected in v4.1.2.0 #12

Closed miehler closed 1 year ago

miehler commented 2 years ago

Hi there,

somehow Setting custom foreign key names in EF Visual Editor for VS 2022 no longer works for me. It did work in VS 2019 though.

Here's the relevant code generated by v3.0.7.2 in VS 2019:

modelBuilder.Entity<global::EFVisualEditorTest.Parent>()
.HasMany<global::EFVisualEditorTest.Child>(p => p.Children)
.WithOne(p => p.Parent)
.HasForeignKey(k => k.PID);
modelBuilder.Entity<global::EFVisualEditorTest.Child>().Navigation(e => e.Parent).IsRequired();

And this is the code generated by v4.1.2.0 in VS 2022:

modelBuilder.Entity<global::EFVisualEditorTest.Parent>()
.HasMany<global::EFVisualEditorTest.Child>(p => p.Children)
.WithOne(p => p.Parent);

HasForeignKey is missing, hence EF Core tries to select a non-existing ParentID column.

Am I doing something wrong or is this a bug?

Kind regards, Axel

jposouza commented 2 years ago

I think this is a bug. Using the same configuration from a project running on VS2019 .NET Core 5 and got the same bug as well

msawczyn commented 2 years ago

I can't reproduce this using the current version. Using the Sandbox_EFCore test project, and adding a foreign key to a relationship, I see the following generated:

         modelBuilder.Entity<global::Sandbox_EF6.Master>()
                     .HasMany<global::Sandbox_EF6.Detail>(p => p.Details)
                     .WithOne()
                     .HasForeignKey(k => k.Fkey)
                     .IsRequired();

Did you set the model property Allow Modeled Foreign Keys to true in the designer properties? Click on the designer background, and it'll be the first property in the Code Generation group.

miehler commented 2 years ago

Hi Michael,

Did you set the model property Allow Modeled Foreign Keys to true in the designer properties?

Yes, I did. I have attached my example project for you, resulting in the differing pieces of generated code mentioned above.

Kind regards, Axel

EFVisualEditorTest.zip

scooterHD commented 2 years ago

Hi there, I have the same problem but only with bidirectional associations. Unidirectional associations work as designed. So I think it's a bug. Kind regards, Scooter

miehler commented 1 year ago

Hi @msawczyn,

do you have any update on this issue?

Best regards, Axel

msawczyn commented 1 year ago

Using the latest version in the Marketplace (4.2.0) and the supplied sample project, I get

         modelBuilder.Entity<global::EFVisualEditorTest.Child>()
                     .ToTable("Child")
                     .HasKey(t => t.ID);
         modelBuilder.Entity<global::EFVisualEditorTest.Child>()
                     .Property(t => t.ID)
                     .ValueGeneratedOnAdd()
                     .IsRequired();
         modelBuilder.Entity<global::EFVisualEditorTest.Child>()
                     .Property(t => t.PID)
                     .IsRequired();
         modelBuilder.Entity<global::EFVisualEditorTest.Child>().HasIndex(t => t.PID);

         modelBuilder.Entity<global::EFVisualEditorTest.Parent>()
                     .ToTable("Parent")
                     .HasKey(t => t.ID);
         modelBuilder.Entity<global::EFVisualEditorTest.Parent>()
                     .Property(t => t.ID)
                     .ValueGeneratedOnAdd()
                     .IsRequired();
         modelBuilder.Entity<global::EFVisualEditorTest.Parent>()
                     .HasMany<global::EFVisualEditorTest.Child>(p => p.Children)
                     .WithOne(p => p.Parent)
                     .HasForeignKey(k => k.PID)
                     .IsRequired();

Possibly something slipped past my QA in the version you're reporting on, but it looks all good now.

Thanks for following up.