Shalin
Shalin

Reputation: 157

How to add many to many relationship through Entity Framework 6, in code?

Hello I have following classes/Models mapped to database using Code-First

public class Speaker
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public string Mobile { get; set; }
    public virtual ICollection<SpeakerCertification> Certifications { get; set; }
}

public class Certification
{
    public int Id { get; set; }
    public string Description { get; set; }
    public virtual ICollection<SpeakerCertification> Speakers { get; set; }
}

public class SpeakerCertification
{
    public int Id { get; set; }
    public int SpeakerId { get; set; }
    public int CertificationId { get; set; }
    public virtual Speaker Speaker { get; set; }
    public virtual Certification Certifications { get; set; }
}

The certification table is already populated with some data. When speaker information is added, the speakers certification must be added as well (which can be chosen through checkboxes). Now I have to add this information to the database. I have written following code and stuck (_ctx is the dbContext). I require the speakerId for speakerCertification table but the speaker is not yet added in the database. The requirement is speakerId must be an identity field. How can I achieve this? The string array selectedCertifications contain the certifications selected when adding the speaker

public bool AddSpeaker(Entities.Speaker speaker, string[] selectedCertifications)
    {
        if (selectedCertifications != null)
        {
            SpeakerCertification speakerCertification = new SpeakerCertification();
            speaker.Certifications = new List<SpeakerCertification>();
            foreach (var certificate in selectedCertifications)
            {
                var certificationToAdd = _ctx.Certifications.Find(int.Parse(certificate));
                speakerCertification.CertificationId = certificationToAdd.Id;
                //speakerCertification.SpeakerId
                speaker.Certifications.Add(speakerCertification);

            }
        }


        _ctx.Speakers.Add(speaker);
        _ctx.SaveChanges();
        return true;
    }

Upvotes: 0

Views: 19

Answers (1)

Shalin
Shalin

Reputation: 157

Sorry for troubling those who went through the question, there was almost no problem at all. I was just too silly to miss it. I forgot the magic of Entity Framework. I implemented the add function in following way

        public bool AddSpeaker(Entities.Speaker speaker, string[] selectedCertifications)
    {
        if (selectedCertifications != null)
        {
            SpeakerCertification speakerCertification = new SpeakerCertification();
            speaker.Certifications = new List<SpeakerCertification>();
            foreach (var certificate in selectedCertifications)
            {

                if (certificate.CompareTo("false")!=0)
                {
                    var certificationToAdd = _ctx.Certifications.Find(int.Parse(certificate));
                    speakerCertification = new SpeakerCertification();
                    speakerCertification.CertificationId = certificationToAdd.Id;
                    speaker.Certifications.Add(speakerCertification);
                }

            }
        }


        _ctx.Speakers.Add(speaker);
        _ctx.SaveChanges();
        return true;
    }

Upvotes: 1

Related Questions