@Eliya Amanoeel, Welcome to Microsoft Q&A, based on your description, you want to achieve the one to one relationship for Activity and Hobby.
I find that I need to make some changes on your many to many relationships so that I could get the output of activities.
Here is a code example you could refer to.
public class Person
{
public int PersonId { get; set; }
public string Name { get; set; }
public virtual ICollection<PersonHobby> personHobbies { get; set; } = new List<PersonHobby>();
}
public class Hobby
{
public int HobbyId { get; set; }
public string Name { get; set; }
public virtual ICollection<PersonHobby> personHobbies { get; set; } = new List<PersonHobby>();
public Activity activity { get; set; }
}
[Keyless]
public class PersonHobby
{
public int PersonId { get; set; }
public Person person { get; set; }
public int HobbyId { get; set; }
public Hobby hobby { get; set; }
}
public class Activity
{
public int ActivityId { get; set; }
public string Name { get; set; }
public int HobbyId { get; set; }
public Hobby hobby { get; set; }
//public virtual ICollection<Person> Persons { get; set; } = new List<Person>();
}
public class MyContext:DbContext
{
public DbSet<Activity> Activitys { get; set;}
public DbSet<Person> Persons { get; set; }
public DbSet<PersonHobby> personHobbies { get; set; }
public DbSet<Hobby> Hobbies { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Data Source=(localdb)\\ProjectModels;Initial Catalog=Test;Integrated Security=True;Connect Timeout=30;Encrypt=False;Trust Server Certificate=False;Application Intent=ReadWrite;Multi Subnet Failover=False");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PersonHobby>().HasKey(h => new { h.PersonId, h.HobbyId });
modelBuilder.Entity<PersonHobby>()
.HasOne(ph => ph.person)
.WithMany(ph => ph.personHobbies)
.HasForeignKey(ph => ph.PersonId);
modelBuilder.Entity<PersonHobby>()
.HasOne(ph => ph.hobby)
.WithMany(ph => ph.personHobbies)
.HasForeignKey(ph => ph.HobbyId);
modelBuilder.Entity<Activity>()
.HasOne(h=>h.hobby)
.WithOne(h=>h.activity)
.HasForeignKey<Hobby>(e => e.HobbyId)
.IsRequired();
}
}
static void Main(string[] args)
{
MyContext context = new MyContext();
Person person1 = new Person();
person1.Name = "Clara";
Person person2 = new Person();
person2.Name = "Jack";
Person person3 = new Person();
person3.Name = "Rose";
Hobby hobby = new Hobby();
hobby.Name = "Arts";
hobby.personHobbies.Add(new PersonHobby { person = person2 });
hobby.personHobbies.Add(new PersonHobby { person = person3 });
Hobby hobby1 = new Hobby();
hobby1.Name = "Sports";
hobby1.personHobbies.Add(new PersonHobby { person = person2 });
hobby1.personHobbies.Add(new PersonHobby { person = person3 });
Hobby hobby2 = new Hobby();
hobby2.Name = "Photography";
hobby2.personHobbies.Add(new PersonHobby { person = person1 });
hobby2.personHobbies.Add(new PersonHobby { person = person3 });
Activity activity1 = new Activity();
activity1.hobby = hobby;
activity1.Name = "act1";
Activity activity2 = new Activity();
activity2.hobby = hobby1;
activity2.Name = "act2";
Activity activity3 = new Activity();
activity3.hobby = hobby2;
activity3.Name = "act3";
context.Activitys.Add(activity1);
context.Hobbies.Add(hobby);
context.Activitys.Add(activity2);
context.Hobbies.Add(hobby1);
context.Activitys.Add(activity3);
context.Hobbies.Add(hobby2);
context.SaveChanges();
foreach (var item in context.Activitys)
{
Console.WriteLine("Activity is "+item.Name);
Console.WriteLine("Hobby is " + item.hobby.Name);
foreach (var p in item.hobby.personHobbies)
{
Console.WriteLine( p.person.Name);
}
Console.WriteLine( "***************");
}
}
Tested result:
Hope my code example could help you.
Best Regards,
Jack
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.