【问题标题】:How can I map two different foreign key with same primary key in different table?如何在不同的表中映射两个具有相同主键的不同外键?
【发布时间】:2012-09-04 10:34:25
【问题描述】:

我有一个名为 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 列进行映射。 p>

这里是 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 中映射两个具有相同主键的外键?

【问题讨论】:

    标签: c# .net asp.net-mvc entity-framework ef-code-first


    【解决方案1】:

    类似这样的:

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

    【讨论】:

    • HasRequired(c => c.Trainee).WithRequiredPrincipal(c => c.Id);.. c.ID 是另一个表的主键。如果我错了,请纠正我?
    • c.ID 中出现错误:- 无法将类型 int 隐式转换为 Model.EnrollTrainee
    • 请检查 Createusers 模型类。
    • 我更新了我的答案。使用 HasRequired(c => c.Trainee).WithRequiredPrincipal(c => c.User_Userid); HasRequired(c => c.Trainer).WithRequiredPrincipal(c => c.User_Userid);
    • 我已经将 Id 更改为 User_Userid ,我收到此错误 Cannot Implicitly convert type int to Model.EnrollTrainee
    【解决方案2】:

    如果您使用的是 MVC3 或 4,那么您可以使用实体框架映射来执行相同的操作,从而生成所需的类。 您可以使用模型浏览器来执行相同的操作。

    你必须做什么:

    创建您的表格 在表中创建 PK 和 FK 约束 右键单击项目中解决方案资源管理器中的模型文件夹 选择添加新项目,从“数据”类别中选择 ADO.Net EF 4.x/5.x 点击确定 它将打开一个向导,提供所需的参数,然后选择您需要的表格。 完成后,它将简单地创建一个 .Edmx 文件,在 .Edmx 的上下文文件中,i

    【讨论】:

    • 您说的是设计优先方法,而我正在研究代码优先方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-13
    • 2015-05-06
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 2018-05-05
    • 1970-01-01
    相关资源
    最近更新 更多