Solved

Seed the Database with Test Data

Posted on 2013-11-23
3
344 Views
Last Modified: 2014-01-27
I was wondering if I could get an explanation of the few line bellow from this code that I found from this tutorial...http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-a-more-complex-data-model-for-an-asp-net-mvc-application

students.ForEach(s => context.Students.AddOrUpdate(p => p.LastName, s));

Explantion?
--------------------------

  var courses = new List<Course>
            {
                new Course {CourseID = 1050, Title = "Chemistry",      Credits = 3,
                  DepartmentID = departments.Single( s => s.Name == "Engineering").DepartmentID,
                  Instructors = new List<Instructor>()
                },


Explantion?
--------------------------



    AddOrUpdateInstructor(context, "Chemistry", "Kapoor");
            AddOrUpdateInstructor(context, "Chemistry", "Harui");


  StudentID = students.Single(s => s.LastName == "Barzdukas").ID,
                    CourseID = courses.Single(c => c.Title == "Chemistry").CourseID,
                    Grade = Grade.B  



Explantion?
--------------------------
foreach (Enrollment e in enrollments)
            {
                var enrollmentInDataBase = context.Enrollments.Where(
                    s =>
                         s.Student.ID == e.StudentID &&
                         s.Course.CourseID == e.CourseID).SingleOrDefault();
                if (enrollmentInDataBase == null)
                {
                    context.Enrollments.Add(e);
                }
            }
            context.SaveChanges();
        }

        void AddOrUpdateInstructor(SchoolContext context, string courseTitle, string instructorName)
        {
            var crs = context.Courses.SingleOrDefault(c => c.Title == courseTitle);
            var inst = crs.Instructors.SingleOrDefault(i => i.LastName == instructorName);
            if (inst == null)
                crs.Instructors.Add(context.Instructors.Single(i => i.LastName == instructorName));
        }
    }



Explantion?
--------------------------
0
Comment
Question by:yguyon28
  • 2
3 Comments
 
LVL 9

Expert Comment

by:QuinnDex
ID: 39672481
the tutorial explains this code and what it is doing, and why you have created an empty list for Instructors.

Read it again go through the code, come back with more specific questions on what you dont understand.
0
 

Author Comment

by:yguyon28
ID: 39672858
I'm not really sure how to understand the sentence bellow....
          students.ForEach(s => context.Students.AddOrUpdate(p => p.LastName, s));
0
 
LVL 9

Accepted Solution

by:
QuinnDex earned 500 total points
ID: 39682882
works in context with other fields, if it exists already then performs update else adds a new record.
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# HTTP GET method sample code 3 57
How would you add MULTITHREADING to the attached C# code? 4 62
Wav problem 4 27
How come this regular expression allows "e"? 14 38
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question