Adding multiple new rows of data to a table based on values in another table

I'm using asp.net and mvc 3 to create my application. Here is a simplified version of my tables.

My tables are as follows

[STUDENT] (ID, NAME)

[CLASS] (ID, SUBJECT)

[CLASSINSTANCE] (ID, SUBJECT, CLASSID)

[CLASSINSTANCEDETAIL] (STUDENTID, CLASSINSTANCEID, ATTENDANCE)

[ENROLLEDIN] (STUDENTID, CLASSID)

I have relationships between student and class

CLASS <- 1 to MANY -> CLASSINSTANCE <- 1 TO MANY -> CLASSINSTANCEDETAIL <- MANY to 1 -> STUDENT

STUDENT <- 1 to MANY -> ENROLLEDIN <- MANY to 1 -> Class

Essentially what I need to do, is evertime I create a new Class, I also need to add a new record in the ClassDetail table with the class id of the class and a matching student id in the enrolledin table for each student. eg. If I created a new classinstance, then I would need to find the classID in that table, and add a new row of data for every student in the ENROLLEDIN table that has a matching classID.

If anyone has any idea how to do this in c# using mvc3 and asp.net, I would be extremely grateful!

PS. I imported my database using the entity framework with my model in a data access layer folder if that makes any difference.

Answers


Well, to create the new ClassDetail records on C# using Linq you could use:

var class = classInstance.Class;
var newDetails = from s in class.Students
                 select new ClassDetail
                 {
                     ClassInstance = classInstance, 
                     Student = s
                 };
foreach(ClassDetail cd in newDetails)
{
    ClassDetails.Add(cd);
}

This assumes that the Student<->Class many-to-many relationship was mapped directly and the EnrolledIn table was not mapped to an entity. If it was, try this:

var class = classInstance.Class;
var newDetails = from ei in class.EnrolledIn
                 select new ClassDetail
                 {
                     ClassInstance = classInstance, 
                     Student = ei.Student
                 };
foreach(ClassDetail cd in newDetails)
{
    ClassDetails.Add(cd);
}

Need Your Help

as3 air app for phone - slight delay tween after button press

actionscript-3 flash

I have noticed a slight delay on my animations when I press a button. The audio will respond asap etc.. but the animation has a second or two delay. I notice it more on the iPhone.

“Transcode” copying video/audio codec

linux ffmpeg h.264 transcode image-stabilization

I took some very shaky video, and went online to try and find how to stabilize it. I came upon this nice blog that uses transcode for it.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.