1

I seem to be stuck on a specific problem. I am using entity framework, and I have auto-generated all the entities from my database. I have noticed that any tables I have that serve as a bridging table to prevent a many-to-many relationship, don't actually have an entity; for example, the table I am trying to use called 'DiplomaCertificate' is linked to a table called 'Degree' through a bridging table called 'DegreeRelationship'. DegreeRelationship takes the primary key of both these tables, and uses them as its primary key, and foreign key. I noticed instead of an entity I get this in my context class:

  modelBuilder.Entity<Degree>()
            .HasMany(e => e.DiplomaCertificates)
            .WithMany(e => e.Degrees)
            .Map(m => m.ToTable("DegreeRelationship").MapLeftKey("DegreeID").MapRightKey("ProgramID"));

In my code behind, I have so far been handling processing inserts as follows:

public void InsertDiplomaProgram(DiplomaCertificate diplomaProgram, List<EntranceRequirementList> entReqList, List<int> degreeIDs)
    {
        using (Pathway_Model context = new Pathway_Model())
        {
            DiplomaCertificate added = null;
            EntranceRequirement addedEntReq = null;
            added = context.DiplomaCertificates.Add(diplomaProgram);


            // create entrance requirement entry for each row entered
            foreach (EntranceRequirementList entry in entReqList)
            {
                EntranceRequirement entReq = new EntranceRequirement()
                {
                    ProgramID = added.ProgramID,
                    CourseID = entry.CourseID,
                    Marks = entry.Mark
                };

                addedEntReq = context.EntranceRequirements.Add(entReq);
            }

            //create degree diploma entry for each row entered
            foreach (int entry in degreeIDs)
            { 
                // normally I would try and use a foreach to populate new entries in the database,
                // but right now I am not sure???
            }



            // commits the add to the databas
            context.SaveChanges();
        }
    }

Basically I am trying to take the diplomaID, and go through and combine the list of degree ID's into the DegreeRelationship table, but I am stuck here. If anyone could offer a suggestion I would greatly appreciate it. Thank You!!

0

1 Answer 1

0

I am assuming below will work.

diplomaProgram.Degrees.Add(Degree)

It does make sense to use this structure as well. So DiplomaProgram will have its mapped Degree (vice versa) in its collection.

More Details

2
  • I get an error here saying that I am trying to insert into the diplomacertificate table, when it should be inserting into degreerelationship Mar 9, 2015 at 17:58
  • I jsut had to change things around - i had to put selectedDegree.DiplomaCertificates.Add(diplomaProgram); as the keys were mapped differently. Mar 9, 2015 at 18:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.