首页 文章

如何在不同的表中映射具有相同主键的两个不同的外键?

提问于
浏览
0

我有一个名为 EnrollTrainee 的表 . 我的EnrollTrainee模型类中有这些列:

[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; }

这两个列 TraineeIDTrainerID 将与表 CreateUser 中的 User_id 列进行映射 .

这是 CreateUser 模型类

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; }

 }

如何在EF CF中映射具有相同主键的两个外键?

2 回答

  • 2

    像这样的东西:

    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());
              ......
         }
         ....
    }
    
  • -1

    如果您使用的是MVC3或4,那么您可以使用Entity Framework映射来执行相同操作,这反过来将生成所需的类 . 您可以使用模型浏览器执行相同操作 .

    你要做什么:

    创建表在表中创建PK和FK约束右键单击项目中解决方案资源管理器中的Model文件夹,选择Add New Item,从“Data”类别中选择ADO.Net EF 4.x / 5.x单击Ok It将打开一个向导,提供所需的参数,在路上选择你要求的表格 . 完成后,它将简单地在.Edmx的上下文文件中创建一个.Edmx文件

相关问题