You don’t need to save these two objects separately, You can achieve this by having on save call only
public class Student{
[Key]
public int Studentid {get; set;}
public string fname{get; set;}
public string lname{get; set;}
public virtual ICollection<Enrollement> Enrollement_E{ get; set; }
}
public class Enrollement{
[Key]
public int EnrolId {get; set;}
[ForeignKey("Studentid ")]
public int Student{get; set;}
public string kk{get; set;}
public virtual Student Student_E { get; set; }
}
Inside the connected OnModelCreating method , define the relation like, it will make sure when you have Student object, its related or child objects i.e Enrollements will also be saved
modelBuilder.Entity<Student>()
.HasMany(c => c. Enrollement_E)
.WithOne(e => e. Student_E);
modelBuilder.Entity<Enrollement>().Ignore(x => x. Student_E);
Now your code should look like this
try{
student _student = new student{fname = "fname", lname="lname"};
_student.Enrollement_E = new List< Enrollement>();
_student.Enrollement_E.add(new Enrollement{student=???, kk="test"});
repository.stu.insert(_student);
}
finally
{
repository.save();
}
This is done using Entity Framework Core
one last thing, following conventions you also need to change the foreign Key field
public int Student{get; set;} to public int StudentId{get; set;}