简体   繁体   中英

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

My Entities look like -

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

I would like to have entites as above but the SQL Table should be One. The Table should be "DoctorSchedule" with the columns are "Id","DoctorId","Day","StartTime","EndTime".

Please suggest the code using Data Annotations or FluentAPI.

From the research it would appear that in order to have more than one entity per table, they need to inherit one another. I'm not sure if this will allow you to do what you want, but I tested it, and entity framework didn't complain, and the tables were successfully created. Again, this worked and allowed the models and relationships to be created, because I don't know what your needs are you'll have to test it yourself to see if it does what you need it to do. Best of luck!

You also appeared to want a Doctor entity, but provided no information about it, so I didn't attempt to do anything with a Doctor class.

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

Here's the DbContext/Fluent API I tried

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


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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