实体框架代码首先使用相同的数据库表进行一对多关系

时间:2014-06-21 17:32:23

标签: .net entity-framework orm ado.net

我的实体看起来像 -

[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建议代码。

1 个答案:

答案 0 :(得分:0)

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

你似乎也想要一个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);


}
相关问题