简体   繁体   中英

EF6 Required-to-Optional Relationship (One-to–Zero-or-One) not working correctly

I have a one-to-zero-or-one relationship and the problem that I'm running into is that the navigation property on the dependent entity to the principal entity is always null unless the principal entity is loaded first from its DbSet.

Database Relationship is Employee.ID -> TrainerProfile.TrainerID with TrainerID being the primary key and foreign key.

Entities are Employee.Id -> TrainerProfile.Id that is mapped with the [Column] attribute.

//Principal
public class Employee : BaseEntity<int>
{
    [Key]
    [Column("ID")]
    public override int Id { get; protected set; }

    [Required(ErrorMessage = "A Username is required")]
    [DataType(DataType.Text)]
    [StringLength(256)]
    public string UserName { get; set; }

    [Required(ErrorMessage = "A First Name is required")]
    [StringLength(40)]
    public string FName { get; set; }

    [Required(ErrorMessage = "A Last Name is required")]
    [StringLength(40)]
    public string LName { get; set; }
    ...
}

//Dependent
public class TrainerProfile : BaseEntity<int>
{   
    private TrainerProfile()
    {
    } 

    protected TrainerProfile(int id) : base(id)
    {
    }

    [Key]
    [Column("TrainerID")]
    public override int Id { get; protected set; }

    public bool Passport { get; set; }

    [StringLength(1000)]
    public string SpecialConsiderations { get; set; }

    [StringLength(10)]
    public string SeatPreference { get; set; }

    [ForeignKey("Id")]
    public virtual Employee Employee { get; set; }
}


//DBContext OnModelCreating()
modelBuilder.Entity<Employee>()
            .HasOptional(e => e.TrainerProfile)
            .WithRequired(e => e.Employee);

modelBuilder.Entity<TrainerProfile>()
            .HasKey(e => e.Id)
            .HasRequired(e => e.Employee)
            .WithOptional(e => e.TrainerProfile);

UPDATE

var db = new DBContext();
var profile = db.TrainerProfiles.First(); //profile.Employee null
var employee = db.Employees.List(); //profile.Employee now loaded

I was able to solve the issue by changing the default constructor accessor from private to protected.

//Dependent
public class TrainerProfile : BaseEntity<int>
{   
    protected TrainerProfile() //***Changed from private to protected***
    {
    } 

    protected TrainerProfile(int id) : base(id)
    {
    }

    [Key]
    [Column("TrainerID")]
    public override int Id { get; protected set; }

    public bool Passport { get; set; }

    [StringLength(1000)]
    public string SpecialConsiderations { get; set; }

    [StringLength(10)]
    public string SeatPreference { get; set; }

    [ForeignKey("Id")]
    public virtual Employee Employee { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM