[RESOLVED]How to create a new record to a junction table

I need to post to a junction table Entity Framework generated(CustomerEmployeeJobs). Every Job can have multiple Customer Employees. Every Job must have a CustomerEmployee PM, CustomerEmployee Account, CustomerEmployee Admin and a CustomerEmployee Superintendent.Right
now those are defined with the CustomerEmployeeRole property. When I create a new Job I have select boxes that display the Customer Employees with there appropriate title. So how do I make this POST? I assumed this relationship would be a many-to-many. The
Junction table has a JobId and CustomerEmployeeId columns. So It seems like that is correct. I can manually add the same JobId with multiple CustomerEmployeeId’s. But how do i do that through the actual application? Here is a plunkr of what the New Job Modal
looks like. Would it be better if I changed how I store the CustomerEmployee titles? Instead of "string" CustomerEmployeeRole it could be booleans, "bool" CustomerEmployeeIsPM, CustomerEmployeeIsAdmin, etc…. here is a plunker of what the new Job modal looks
like http://plnkr.co/edit/5jpvCe38mksrm0fnkEHn?p=preview

public class Job
{
    //job
    public int JobId { get; set; }
    public int? JobNumber { get; set; }
    public string JobName { get; set; }

    public virtual ICollection<CustomerEmployee> CustomerEmployees { get; set; }
}
public class CustomerEmployee
{
    [Key]
    public int CustomerEmployeeId { get; set; }
    public string CustomerEmployeeFirstName { get; set; }
    public string CustomerEmployeeLastName { get; set; }
    public string CustomerEmployeeRole { get; set; }


    public virtual ICollection<Job> Jobs { get; set; }
}

// POST api/<controller>
    public async Task<IHttpActionResult> PostnewJob([FromBody]JobViewModel newJob)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        using (var context = new ApplicationDbContext())
        {
            var job = new Job();

            Mapper.CreateMap<JobViewModel, Job>();
            Mapper.Map(newJob, job);

            context.Jobs.Add(job);

            await context.SaveChangesAsync();

            return CreatedAtRoute("JobApi", new { job.JobId }, job);
        }

Hi,

texas697

When I create a new Job I have select boxes that display the Customer Employees with there appropriate title. So how do I make this POST

Base on the CustomerEmployee primary key (Selected dropdownList value) to retrieve this record and add the new Job to it.

texas697

I assumed this relationship would be a many-to-many. The Junction table has a JobId and CustomerEmployeeId columns. So It seems like that is correct. I can manually add the same JobId with multiple CustomerEmployeeId’s. But how do i do that through the actual
application?

For the many-to many relationship you could refer to:

# Configuring Relationships with the Fluent API

http://msdn.microsoft.com/en-in/data/jj591620.aspx

texas697

Would it be better if I changed how I store the CustomerEmployee titles? Instead of "string" CustomerEmployeeRole it could be booleans, "bool" CustomerEmployeeIsPM, CustomerEmployeeIsAdmin

You could have a Role table, then associate CustomerEmployee with
Role table base on the foreign key.

Best Regards

Starain

Leave a Reply