I'm trying to figure out why when I insert a JobOffert record into my MS SQL Server Database I'm getting a duplicated record of My Professional that is an attribute of JobOffert, let me show my code:
Here is my controller Post Method:
[HttpPost]
public ActionResult CreateJobOffert(JobOffertModel jobOffert, FormCollection data)
{
initBusinessObjects();
Util.Util.ChangeContextInstance(customerBusiness, null, skillBusiness, jobOffertBusiness);
var allSkills = skillBusiness.GetAllSkills();
var allProfessionals = professionalBusiness.GetAllElements();
//Se eu recarregar a mesma página eu tenho que carregar os ViewBags
if (ModelState.IsValid)
{
var customerId = int.Parse(data["customerId"]);
var professionalId = int.Parse(data["professionalsList"]);
var customer = customerBusiness.GetById(customerId);
var professional = professionalBusiness.GetById(professionalId);
var skillsIds = data["requiredSkills"].Split(',').Select(x => int.Parse(x));
jobOffert.Skills = allSkills.Where(x => skillsIds.Contains(x.Id)).ToList();
jobOffert.Customer = customer;
if (jobOffert.Active)
{
jobOffert.Professional = professional;
}
jobOffertBusiness.Insert(jobOffert);
return View("Edit", customer);
}
return View("CreateJobOffert", jobOffert);
}
Here I return My Professional by its ID:
public ProfessionalModel GetProfessionalById(int id)
{
var professional = Context.ProfessionalContext
.Include(x => x.UserAccount)
.Include(x => x.UserAddress)
.Include(x => x.Skills)
.Include(x => x.Skills.Select(y => y.Category))
.Include(x => x.Tasks)
.FirstOrDefault(x => x.Id == id);
return professional;
}
That's curious because My Customer, another attribute of JobOffert is not duplicated, here I got my customer?
public CustomerModel GetCustomerById(int id)
{
var customer = Context.CustomerContext
.Include(x => x.UserAccount)
.Include(x => x.UserAddress)
.Include(x => x.JobOfferts)
.FirstOrDefault(x => x.Id == id);
return customer;
}
And this is my JobOffertModel:
public class JobOffertModel
{
[Key]
public int Id { get; set; }
public ProfessionalModel Professional { get; set; }
[Required]
public string Description { get; set; }
[Required]
[DefaultValue(false)]
public bool Acepted { get; set; }
[Required]
[DefaultValue(true)]
public bool Active { get; set; }
public ICollection<SkillModel> Skills { get; set; }
[Required]
[Column(TypeName = "DateTime2")]
public DateTime JobDate { get; set; }
public virtual CustomerModel Customer { get; set; }
}
Any Idea? How can I fix this and stop to duplicate my professional records?
Thanks!
EDIT
Hi, I got good news, I did it, but the bad news is that I've no idea why it works. I just must to select again my professional, but this time, when I'm one step to store my data, here is my old Insert JobOffert method:
public void Insert(JobOffertModel jobOffertModel)
{
Context.JobOffertContext.Add(jobOffertModel);
Context.SaveChanges();
}
And here my new Insert JobOffert method:
public void Insert(JobOffertModel jobOffertModel)
{
var professional = Context.ProfessionalContext.Include(x => x.UserAccount)
.Include(x => x.UserAddress)
.Include(x => x.Skills)
.Include(x => x.Skills.Select(y => y.Category))
.Include(x => x.Tasks)
.FirstOrDefault(x => x.Id == jobOffertModel.Professional.Id);
jobOffertModel.Professional = professional;
Context.JobOffertContext.Add(jobOffertModel);
Context.SaveChanges();
}
So, now I change my question, why using second approach it works and using the first one it doesn't?
Thanks again!