I have a table named as EnrollTrainee. I have these columns in my EnrollTrainee Model Class:
[Key]
public int id { get; set; }
public int TraineeID { get; set; }
public int TrainerID { ![enter image description here][1]get; set; }
public virtual CreateUsers user_userid { get; set; }
public virtual CreateUsers user_id { get; set; }
public DateTime dt { get; set; }
These two Columns TraineeID and TrainerID are to be mapped with User_id Column in table CreateUser.
Here is CreateUser Model Class
public class CreateUsers
{
[Key]
public int User_Userid { get; set; }
[Required]
[Display(Name="Enter User Name")]
public string User_name { get; set; }
[Required]
[Remote("IsDomainIDExist", "Account", ErrorMessage = "Domain ID Already Exist")]
[Display(Name = "Enter Domain ID")]
public string User_username { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Enter Password")]
public string User_password { get; set; }
[Required]
[Display(Name = "Enter Department")]
public string User_department { get; set; }
[Required]
[Remote("IsEmployeeIDExist", "Account", ErrorMessage = "Domain ID Already Exist")]
[Display(Name = "Enter Employee ID")]
public string User_employeeid { get; set; }
[Required]
[Display(Name="Select Role Type")]
public int RoleID { get; set; }
[Display(Name="Enable?")]
public bool User_Enable { get; set; }
[DataType(DataType.DateTime)]
[Display(Name = "Date Time")]
public DateTime dt { get; set; }
public virtual RoleModel Role { get; set; }
}
How can I map two Foreign Key with same Primary Key in EF CF?
Something like this:
public class EnrollTrainee
{
[Key]
public int id { get; set; }
public int TraineeID { get; set; }
public virtual CreateUser Trainee { get; set; }
public int TrainerID { get; set; }
public virtual CreateUser Trainer { get; set; }
public DateTime dt { get; set; }
}
internal class EnrollTraineeConfiguration:EntityTypeConfiguration<EnrollTrainee>
{
public EnrollTraineeConfiguration()
{
ToTable("EnrollTrainee");
Property(c => c.dt).HasColumnName("dt");
Property(c => c.TraineeID).HasColumnName("TraineeID");
Property(c => c.TrainerID).HasColumnName("TrainerID");
HasKey(c => c.id);
HasRequired(c => c.Trainee).WithMany().HasForeignKey(c=>c.TraineeId);
HasRequired(c => c.Trainer).WithMany().HasForeignKey(c => c.TrainerId);
}
}
public class Context: DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new EnrollTraineeConfiguration());
......
}
....
}