0

With the code below when I update the object Employee a new record is added in the Language table and I don't understand why. The LanguageId has a value and I set the Language (see info in the code). The Employee record is updated with as LanguageId the newly created.

What I do wrong ?

public class Employee
{
    public virtual int EmployeeId { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }

    public virtual int LanguageId { get; set; }
    public virtual Language Language { get; set; }
}

public class Language
{
    public virtual int LanguageId { get; set; }
    public virtual string Name { get; set; }
    public virtual string Code { get; set; }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Employee>()
    .HasRequired(t => t.Language); 
}

private void Update(Employee employee)
{
    //LanguageId in employee object has a value, OK
    //Language is null
    employee.Login = employee.Login.ToUpper();
    employee.Language = myRepository.Get(employee.LanguageId);

    myRepository.Update(employee);
}

2 Answers 2

1

There are some tips that I want to share with you. If you want to configure a one to one relationship using a FK, Entity Framework requires that the primary key of the dependent end also be the foreign key, but I think this is not your case because, one Language can be associated to more than one Employee, so you need to configure a one-to-many relationship. The configuration of your relationship would be like this:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
   modelBuilder.Entity<Employee>().HasRequired(t => t.Language).
                                   WithMany().HasForeignKey(e=> e.LanguageId);
}

If you want update the Language in a Employee, you have two options:

  • Setting the FK property LanguageId
  • Setting the navigation propertyLanguage.

If you already set the LanguageId with the Id of a Language that already exist in DB, you don't need to find that Language and set the Language property in your Employee instance. Setting the FK property is enough to EF to make the change.

0

You have wrong code.

public class Employee
{
    public int EmployeeId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public int LanguageId { get; set; }
    [ForeignKey("LanguageId")]
    public virtual Language Language { get; set; }
}
public class Language
{
    public int LanguageId { get; set; }
    public string Name { get; set; }
    public string Code { get; set; }
}

If emplyee.LanguageId has value then EF bind Language object autamatically.

If you want to save Employee without language property then you have to make it optional in OnModelCreating and in Employee class:

public int? LanguageId {get; set;}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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