Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing ForaignKey on 1 <-> 0..1 #25

Closed
Mr-Pearce opened this issue Aug 9, 2022 · 3 comments
Closed

Missing ForaignKey on 1 <-> 0..1 #25

Mr-Pearce opened this issue Aug 9, 2022 · 3 comments
Labels
no repro Can't reproduce. Need sample code that exhibits the issue.
Milestone

Comments

@Mr-Pearce
Copy link

Unchanged
grafik

Created DbContext Code:
v4.0.1.0

modelBuilder.Entity<Upload>()
		 .HasOne<Commit>(p => p.Commit)
		 .WithOne(p => p.Upload)
		 .HasForeignKey("Commit", "UploadId")
		 .OnDelete(DeleteBehavior.Cascade);

v4.1.2.0

modelBuilder.Entity<Upload>()
			 .HasOne<Commit>(p => p.Commit)
			 .WithOne(p => p.Upload);

Error v4.1.2.0

System.InvalidOperationException: The dependent side could not be determined for the one-to-one relationship between 'Commit.Upload' and 'Upload.Commit'. 
To identify the dependent side of the relationship, configure the foreign key property. If these navigations should not be part of the same relationship, configure them independently via separate method chains in 'OnModelCreating'. See http://go.microsoft.com/fwlink/?LinkId=724062 for more details.

When i ad the missing two lines back it works like before.

Working but still strange
grafik

v4.0.1.0

modelBuilder.Entity<Program>()
		 .HasMany<Upload>(p => p.Uploads)
		 .WithOne(p => p.Program)
		 .HasForeignKey("ProgramId")
		 .OnDelete(DeleteBehavior.NoAction)
		 .IsRequired();

v4.1.2.0

modelBuilder.Entity<Program>()
		 .HasMany<Upload>(p => p.Uploads)
		 .WithOne(p => p.Program);

Is this behavior expected?

@msawczyn msawczyn added the investigating Looking into this label Aug 13, 2022
@msawczyn msawczyn added this to the 4.2.1 milestone Aug 13, 2022
@msawczyn
Copy link
Owner

Using the latest version (4.2.0) I made the following model:
image

It generated the code

         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>()
                     .HasOne<global::EFVisualEditorTest.Child>(p => p.Child)
                     .WithOne(p => p.Parent)
                     .HasForeignKey<global::EFVisualEditorTest.Child>(k => k.PID);

which migrated fine and created the database.

Possibly the issue slipped by in a prior version but is fixed now. Can you give it another try?

@msawczyn msawczyn added no repro Can't reproduce. Need sample code that exhibits the issue. and removed investigating Looking into this labels Aug 14, 2022
@Mr-Pearce
Copy link
Author

The Entities above are fine now.
But i found another "inconsistency"
Bidirectinal Associations generate different code depending on the Start and End point even when you set the Multiplicity the same.
Although it looks identical in the Model Diagram.

Left: Bidirectional from Upload -> Release
Right: Bidirectional from Release -> Upload with switched Multiplicity
It looks nearly the same in the Model Diagram, shouldn't this create identical modelBuilder Code?

grafik

@msawczyn
Copy link
Owner

That's a side effect of how the T4 iterates through the classes. The resulting code is functionally identical, ignoring the change resulting from the removal in the model of the UploadId foreign key property. There isn't a need to change what it does since it produces the same model in Entity Framework Core.

Of course, since this processing happens in the T4, you're free to change it if it's bothersome. But it doesn't worry me from a product functionality standpoint.

Thanks for pointing it out. Since the original issue is resolved, I'll close this ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no repro Can't reproduce. Need sample code that exhibits the issue.
Projects
None yet
Development

No branches or pull requests

2 participants