How to save multiple model to Db Context (EF)

I am stuck on saving my model on Db Context, I am not sure where to start or how I will proceed.

I have 3 Models (This is a strip version for easier reading)

public class Student
{
    [Required]
    public int Id { get; set; }
    public virtual List<SyTerm> SyTerm { get; set; }
}

public class SyTerm
{
    [Required]
    public int Id { get; set; }
    public virtual List<Course> Courses { get; set; }
}
public class Course
{
    [Required]
    public int Id { get; set; }
}

My problem here is when I try to save it in using Entity Framework

        db.Courses.Add(cortemp1);
        db.Courses.Add(cortemp2);

        db.Students.Add(stud);
        ...
        db.SaveChanges();

I always get an error (Various Error like conflicting, etc.) I am not sure the right sequence on how would I save it.

For example I have this

    var stud = new Student();
    var sys = new List<SyTerm>();
    var cours = new List<Course>();

    var sytemp1 = new SyTerm();
    var sytemp2 = new SyTerm();

    var cortemp1 = new Course();
    var cortemp2 = new Course();

How should I sequence it to save it in my Db?

Upvotes: 0

Views: 1837

Answers (3)

Avinash Jain
Avinash Jain

Reputation: 7606

When adding/updating Entity Framework entities, you have to make sure you consider the DB foreign key relation and map it correctly. In you case SysTerm have list of Courses, but course should also have one SysTerm Id property, to make sure correct one to many relation maintained. Sample shown below how to create entity relationship in correct way

public class Student
{
    public Student()
    { 

    }
    public int StudentID { get; set; }
    public string StudentName { get; set; }

    //Foreign key for Standard
    public int StandardRefId { get; set; }

    [ForeignKey("StandardRefId")]
    public Standard Standard { get; set; }
}

public class Standard
{
    public Standard()
    { 

    }
    public int StandardId { get; set; }
    public string StandardName { get; set; }

    public ICollection<Student> Students { get; set; }

 }

Please check this answer for more details : Understanding ForeignKey attribute in entity framework code first

Upvotes: 0

Anshul Nigam
Anshul Nigam

Reputation: 1628

First of all, you dont need multiple save changes if you have you entity relationship in correct way so do something like this

var stud = new Student();
var sys = new List<SyTerm>();
var cours = new List<Course>();



var sytemp1 = new SyTerm();
var sytemp2 = new SyTerm();

sys.Add(sytemp1);//add data to collection
var cortemp1 = new Course();
var cortemp2 = new Course();


  stud.SysTerm = sys;// here we are saying that student entity has this collection

 dbCtx.Student.add(stud); // just add student
 dbCtx.SaveChanges()

In simple terms add all object to student then just add it and save changes as student is now one complete entity. For exact sample look at http://www.entityframeworktutorial.net/code-first/simple-code-first-example.aspx

Upvotes: 1

BeardedMan
BeardedMan

Reputation: 394

I think you should have relation between Course and SyTerm, so your Course should look like:

public class Course
{
   [Required]
   public int Id { get; set; }
   public virtual SyTerm { get; set; }
}

then of course you have to initialize it if it is required

Upvotes: 0

Related Questions