Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
I tried to replicate your case and the only workable solution I could find is to use OnModelCreating configuration (fluent style) instead of attributes: public class Contestant { public int Id...
Answer
#2: Post edited
- I tried to replicate your case and the only workable solution I could find is to use OnModelCreating configuration (fluent style) instead of attributes:
- ```c#
- public class Contestant
- {
- public int Id { get; set; }
- public int AvatarID { get; set; }
- public virtual Picture? Avatar { get; set; }
- public virtual ICollection<Picture>? Pictures { get; set; }
- }
- public class Picture
- {
- public int Id { get; set; }
- public int ContestantID { get; set; }
- public virtual Contestant? Contestant { get; set; }
- public virtual Contestant? ContestantInverse { get; set; }
- }
- modelBuilder.Entity<Contestant>()
- .HasOne(u => u.Avatar)
- .WithOne(r => r.Contestant)
- .HasForeignKey<Contestant>(u => u.AvatarID)
- .HasConstraintName("FK_Contestant_Avatar")
- .OnDelete(DeleteBehavior.Restrict);
- modelBuilder.Entity<Picture>()
- .HasOne(d => d.ContestantInverse)
- .WithMany(p => p.Pictures)
- .HasConstraintName("FK_Picture_Contestant")
- .OnDelete(DeleteBehavior.Restrict);
- ```
The `Picture` needs two navigation properties related to `Contestant` since it has two relations with it (1:1 and n:1).
- I tried to replicate your case and the only workable solution I could find is to use OnModelCreating configuration (fluent style) instead of attributes:
- ```c#
- public class Contestant
- {
- public int Id { get; set; }
- public int AvatarID { get; set; }
- public virtual Picture? Avatar { get; set; }
- public virtual ICollection<Picture>? Pictures { get; set; }
- }
- public class Picture
- {
- public int Id { get; set; }
- public int ContestantID { get; set; }
- public virtual Contestant? Contestant { get; set; }
- public virtual Contestant? ContestantInverse { get; set; }
- }
- modelBuilder.Entity<Contestant>()
- .HasOne(u => u.Avatar)
- .WithOne(r => r.Contestant)
- .HasForeignKey<Contestant>(u => u.AvatarID)
- .HasConstraintName("FK_Contestant_Avatar")
- .OnDelete(DeleteBehavior.Restrict);
- modelBuilder.Entity<Picture>()
- .HasOne(d => d.ContestantInverse)
- .WithMany(p => p.Pictures)
- .HasConstraintName("FK_Picture_Contestant")
- .OnDelete(DeleteBehavior.Restrict);
- ```
- The `Picture` needs two navigation properties related to `Contestant` since it has two relations with it (1:1 and n:1).
- As a side note, while liking the attributes more, I decided to use the fluent syntax only because some configurations are not supported by the attributes (e.g. index on 2+ properties), and having the configuration split in two can lead to confusion.
#1: Initial revision
I tried to replicate your case and the only workable solution I could find is to use OnModelCreating configuration (fluent style) instead of attributes: ```c# public class Contestant { public int Id { get; set; } public int AvatarID { get; set; } public virtual Picture? Avatar { get; set; } public virtual ICollection<Picture>? Pictures { get; set; } } public class Picture { public int Id { get; set; } public int ContestantID { get; set; } public virtual Contestant? Contestant { get; set; } public virtual Contestant? ContestantInverse { get; set; } } modelBuilder.Entity<Contestant>() .HasOne(u => u.Avatar) .WithOne(r => r.Contestant) .HasForeignKey<Contestant>(u => u.AvatarID) .HasConstraintName("FK_Contestant_Avatar") .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity<Picture>() .HasOne(d => d.ContestantInverse) .WithMany(p => p.Pictures) .HasConstraintName("FK_Picture_Contestant") .OnDelete(DeleteBehavior.Restrict); ``` The `Picture` needs two navigation properties related to `Contestant` since it has two relations with it (1:1 and n:1).