实体框架关系两个表

时间:2017-02-10 15:50:34

标签: c# entity-framework ef-code-first

我正在尝试创建一个友谊映射表,其中有2个FK来自同一个类(User和SportsFacility)

以下是我的模特课程。

public partial class User
{
    [Key]
    public int Id { get; set; }

    [Required]
    public int AcountId { get; set; }

    [Required]
    [MaxLength(20)]
    //[Column(TypeName = "varchar")]
    public string Login { get; set; }

    [Required]
    [MaxLength(20)]
    public string Name { get; set; }

    [Required]
    public string Password { get; set; }

    public virtual ICollection<CheckQueue> ChecksQueue { get; set; }

    public virtual ICollection<BookedEvent> BookedEvents { get; set; }
}

public partial class SportsFacility
{
    public SportsFacility()
    {
        ChecksQueue = new List<CheckQueue>();
        BookedEvent = new List<BookedEvent>();
    }

    [Key]
    public int Id { get; set; }

    [Required]
    [MaxLength(30)]
    public string Name { get; set; }

    [Required]
    public int PnH_Id { get; set; }

    [Required]
    [MaxLength(70)]
    public string Address { get; set; }

    public int City_Id { get; set; }

    [ForeignKey("City_Id")]
    public virtual City City { get; set; }

    public virtual ICollection<CheckQueue> ChecksQueue { get; set; }

    public virtual ICollection<BookedEvent> BookedEvent { get; set; }
}


public partial class CheckQueue
{        
    [Key]
    public int Id { get; set; }

    public DateTime StartCheckTime { get; set; }

    [Required]
    public string CheckParams { get; set; }

    public string Error { get; set; }

    public bool IsDeleted { get; set; }

    public int User_Id { get; set; }

    [ForeignKey("User_Id")]
    public virtual User User { get; set; }

    public int SportsFacility_Id { get; set; }

    [ForeignKey("SportsFacility_Id")]
    public virtual SportsFacility SportsFacility { get; set; }

    [Index]
    public DateTime? LastCheck { get; set; }
}

public partial class BookedEvent
{
    [Key]
    public int Id { get; set; }

    public DateTime BookDate { get; set; }

    [Required]
    public string CheckParams { get; set; }

    public string Error { get; set; }

    public bool IsDeleted { get; set; }

    public int User_Id { get; set; }

    [ForeignKey("User_Id")]
    public virtual User User { get; set; }

    public int SportsFacility_Id { get; set; }

    [ForeignKey("SportsFacility_Id")]
    public virtual SportsFacility SportsFacility { get; set; }
}

我尝试覆盖DbContext中的OnModelCreating方法:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<City>();

        modelBuilder.Entity<SportsFacility>();

        modelBuilder.Entity<User>();

        modelBuilder.Entity<CheckQueue>();

        modelBuilder.Entity<BookedEvent>();

        base.OnModelCreating(modelBuilder);

}

这会导致显示新的错误消息。

Additional information: The entity types 'BookedEvent' and 'CheckQueue' cannot share table 'CheckQueues' because they are not in the same type hierarchy or do not have a valid one to one foreign key relationship with matching primary keys between them. 

非常感谢任何帮助。

0 个答案:

没有答案