繁体   English   中英

实体框架代码优先与同一数据库表一对多关系

[英]Entity Framework code first One to Many Relationship with same database table

我的实体看起来像-

[Table("DoctorSchedule")]
    public class DoctorSchedule
    {
        [Key]
        public int Id {get; set;}

        [Column("DoctorId")]
        public virtual Doctor Doctor { get; set; }
        public virtual ICollection<Schedule> Schedules { get; set; }
    }

public class Schedule
    {            
        public DayOfWeek Day { get; set; }
        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }

        [Required]
        public DoctorSchedule DoctorSchedule { get; set; }
    }

我希望拥有上述实体,但SQL表应为One。 该表应为“ DoctorSchedule”,其列为“ Id”,“ DoctorId”,“ Day”,“ StartTime”,“ EndTime”。

请使用数据注释或FluentAPI建议代码。

从研究看来,为了使每个表具有多个实体,它们需要彼此继承。 我不确定这是否可以做您想做的事情,但是我对其进行了测试,并且实体框架没有抱怨,并且表已成功创建。 同样,这可行,并允许创建模型和关系,因为我不知道您的需求是什么,您必须自己对其进行测试,以查看它是否满足您的要求。 祝你好运!

您似乎还想要一个Doctor实体,但是没有提供任何有关该实体的信息,因此我没有尝试对Doctor类做任何事情。

public class DoctorSchedule
{

    public int DoctorSchedule_Id { get; set; }

    //[Column("DoctorId")] 
    //public virtual Doctor Doctor { get; set; } 
    public virtual ICollection<Schedule> Schedules { get; set; }
}

public class Schedule : DoctorSchedule
{
    public DayOfWeek Day { get; set; }
    public DateTime StartTime { get; set; }
    public DateTime EndTime { get; set; }

    public int Schedule_Id { get; set; }
    public int DoctorSchedule_FK { get; set; }


    public DoctorSchedule DoctorSchedule { get; set; }
}

这是我尝试过的DbContext / Fluent API

public class DocContext : DbContext
{

    public DbSet<DoctorSchedule> DoctorSchedules { get; set; }
    public DbSet<Schedule> Schedules { get; set; }

    protected override void OnModelCreating(DbModelBuilder mb)
    {

        //DoctorSchedule Mappings
        //
        mb.Entity<DoctorSchedule>()
            .ToTable("DoctorSchedule")
            .HasKey(ds => ds.DoctorSchedule_Id);

        mb.Entity<DoctorSchedule>()
            .Property(ds => ds.DoctorSchedule_Id)
            .HasColumnName("DoctorSchedule_Id")
            .HasColumnType("int")
            .IsRequired()
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

        mb.Entity<DoctorSchedule>()
            .HasMany(ds => ds.Schedules)
            .WithRequired(s => s.DoctorSchedule)
            .HasForeignKey(s => s.DoctorSchedule_FK);


        //Schedule Mappings
        //
        mb.Entity<Schedule>()
            .ToTable("DoctorSchedule")
            .HasKey(s => s.Schedule_Id); //It seems like a compound key is needed

        mb.Entity<Schedule>()
            .Property(ds => ds.Schedule_Id)
            .HasColumnName("Schedule_Id")
            .HasColumnType("int")
            .IsRequired()
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

        mb.Entity<Schedule>()
            .Property(s => s.Day)
            .HasColumnName("Day")
            .IsRequired()
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

        mb.Entity<Schedule>()
            .Property(s => s.StartTime)
            .HasColumnName("StartTime")
            .IsRequired()
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

        mb.Entity<Schedule>()
            .Property(s => s.EndTime)
            .HasColumnName("EndTime")
            .IsRequired()
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

        mb.Entity<Schedule>()
            .Property(ds => ds.DoctorSchedule_FK)
            .HasColumnName("DoctorSchedule_FK")
            .HasColumnType("int")
            .IsRequired()
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);


}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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