实体框架流畅的api使用不是父ID的外键将子映射到父级

时间:2017-10-05 21:32:41

标签: c# .net entity-framework ef-fluent-api

我想映射子类,但子对象上的外键不是Program类上的ID。如何使用EF fluent API来定义这种关系?以下是我的课程。

public class Program
    {
        public int Id { get; set; }
        public string ProgramNumber { get; set; }
        public List<Child> Children { get; set; }
    }

    public class Child
    {
        public int Id { get; set;}
        public string ProgramNumber { get; set; }
    }

    internal class ProgramMapping : EntityTypeConfiguration<Program>
    {
        public ProgramMapping()
        {
            ToTable("PROGRAMS");

            HasKey(t => t.Id);

            Property(t => t.Id).HasColumnName("ID");
            Property(t => t.ProgramNumber).HasColumnName("PROGRAM_NUMBER");
        }
    }

    internal class ChildMapping : EntityTypeConfiguration<Child>
    {
        public ChildMapping()
        {
            ToTable("CHILDREN");

            HasKey(t => t.Id);

            Property(t => t.Id).HasColumnName("ID");
            Property(t => t.ProgramNumber).HasColumnName("PROGRAM_NUMBER");
        }
    }

0 个答案:

没有答案