Entity Framework, Code First is used. There are 2 models - User and Topic. 1 user can have multiple topics.
public class User { public int Id { get; set; } public string Name { get; set; } public virtual ICollection<Topic> Topics { get; set; } } public class Topic { public int Id { get; set; } public string Title { get; set; } public int UserId { get; set; } public User User { get; set; } } It is necessary to make such an addition (topic objects inside the user object):
using (MyDbContext context = new MyDbContext()) { // Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»Ρ Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΈΠΌΠΈ ΡΠΎΠΏΠΈΠΊΠ°ΠΌΠΈ User user = new User { Name = "ExampleName", Topics = new List<Topic> { new Topic { Title = "First topic" }, new Topic { Title = "Second topic" }, } }; // ΠΠ°Π½ΠΎΡΠΈΠΌ Π² Π±Π°Π·Ρ Π²ΡΠ΅ ΡΡΠ°Π·Ρ context.Users.AddOrUpdate(user); // ΠΡΠΎΠΉΠ΄Π΅Ρ Π»ΠΈ Π΄ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅? ΠΠΎΠ±Π°Π²ΡΡΡΡ Π»ΠΈ ΡΠΎΠΏΠΈΠΊΠΈ? context.SaveChanges(); } It is necessary that the user himself and his 2 topics be added, without first resorting to adding the user, and then the topics separately. Is it possible to do something similar using only context.Users.AddOrUpdate(user); Which also already contains topics?