实体框架:覆盖(新)集合和引用属性

时间:2017-03-08 18:55:10

标签: c# sql entity-framework orm entity-framework-6

我有一个实体PersonDetailDetail引用Person(一对多)。我还有Student实体,它继承自Person实体的StudentDetailDetail实体。 当我创建Student的实例时,我想添加Details Student StudentDetail实例的Details。因此,我通过覆盖基类中的属性在Student实体中创建了新的Person集合。还通过覆盖基类中的属性在StudentDetail类中创建了Student引用属性。当我创建新的StudentDetail并将public class Person { public int Id { get; set; } public string Name { get; set; } public string LastName { get; set; } public string Email { get; set; } public string Address { get; set; } public virtual IList<Detail> Details { get; set; } = new List<Detail>(); public virtual Passport Passport { get; set; } } public class Student : Person { public int Course { get; set; } public string Phone { get; set; } public new IList<StudentDetail> Details { get { return base.Details.Cast<StudentDetail>().ToList(); } } } public class Passport { public int Id { get; set; } public string Number { get; set; } public DateTime IssuedDate { get; set; } public DateTime Expires { get; set; } public virtual Person Person { get; set; } public virtual AdditionalInfo AdditionalInfo { get; set; } } public class AdditionalInfo { public int Id { get; set; } public string SuedBefore { get; set; } public string CriminalRecords { get; set; } public virtual Passport Passport { get; set; } } public class Detail { public int Id { get; set; } public string Name { get; set; } public int Count { get; set; } public int OwnerId { get; set; } public virtual Person Owner { get; set; } } public class StudentDetail : Detail { public string StudentStuff { get; set; } public new Student Owner { get { return base.Owner as Student; } set { base.Owner = value; } } } 添加到列表并将其保存到数据库时,所有信息都按预期保存。但是当我尝试从数据库中读回学生时,该集合是空的。这是我的实体。还有一些其他实体,但它们不是必需的:

public class MyEntities : DbContext
{
    public MyEntities()
            : base("data source=localhost;initial catalog=Test1;integrated security=True;")
    {
        Database.Log = s => System.Diagnostics.Debug.WriteLine(s);
    }

    public DbSet<Person> People { get; set; }
    public DbSet<Student> Students { get; set; }
    public DbSet<Passport> Passports { get; set; }
    public DbSet<AdditionalInfo> Additionalinfos { get; set; }
    public DbSet<Detail> Details { get; set; }
    public DbSet<StudentDetail> StudentDetails { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

        Database.SetInitializer(new MyDbContextInitializer());
        // Configure Code First to ignore PluralizingTableName convention 
        // If you keep this convention then the generated tables will have pluralized names. 
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        PersonMapping(modelBuilder);
        StudentMapping(modelBuilder);
        PassportMapping(modelBuilder);
        AdditionalInfotMapping(modelBuilder);
        DetailMapping(modelBuilder);
        StudentDetailMapping(modelBuilder);


    }

    void PersonMapping(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Person>().ToTable("Person");
        modelBuilder.Entity<Person>().HasKey(t => new { t.Id });
        modelBuilder.Entity<Person>().Property(t => t.Id).HasColumnName("Id").HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
        modelBuilder.Entity<Person>().Property(t => t.Name).HasColumnName("Name");
        modelBuilder.Entity<Person>().Property(t => t.LastName).HasColumnName("LastName");
        modelBuilder.Entity<Person>().Property(t => t.Email).HasColumnName("Email");
        modelBuilder.Entity<Person>().Property(t => t.Address).HasColumnName("Address");

        //associations
        modelBuilder.Entity<Person>().HasOptional(p => p.Passport).WithOptionalPrincipal(p => p.Person);
    }

    void StudentMapping(DbModelBuilder modelBuilder)
    {

        modelBuilder.Entity<Student>().ToTable("Student");
        modelBuilder.Entity<Student>().Property(t => t.Course).HasColumnName("Course");
        modelBuilder.Entity<Student>().Property(t => t.Phone).HasColumnName("Phone");
        //modelBuilder.Entity<Student>().Ignore(t => t.Details);
    }

    void PassportMapping(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Passport>().ToTable("Passport");
        modelBuilder.Entity<Passport>().HasKey(t => new { t.Id });
        modelBuilder.Entity<Passport>().Property(t => t.Id).HasColumnName("Id").HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
        modelBuilder.Entity<Passport>().Property(t => t.Number).HasColumnName("Number");
        modelBuilder.Entity<Passport>().Property(t => t.IssuedDate).HasColumnName("IssuedDate");
        modelBuilder.Entity<Passport>().Property(t => t.Expires).HasColumnName("Expires");

        //associations
        modelBuilder.Entity<Passport>().HasOptional(p => p.AdditionalInfo).WithOptionalPrincipal(p => p.Passport);
    }

    void AdditionalInfotMapping(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<AdditionalInfo>().ToTable("Additionalinfo");
        modelBuilder.Entity<AdditionalInfo>().HasKey(t => new { t.Id });
        modelBuilder.Entity<AdditionalInfo>().Property(t => t.Id).HasColumnName("Id").HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
        modelBuilder.Entity<AdditionalInfo>().Property(t => t.SuedBefore).HasColumnName("SuedBefore");
        modelBuilder.Entity<AdditionalInfo>().Property(t => t.CriminalRecords).HasColumnName("CriminalRecords");

        //associations
        //modelBuilder.Entity<Passport>().HasRequired(p => p.Person);
    }

    void DetailMapping(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Detail>().HasKey(t => new { t.Id });
        modelBuilder.Entity<Detail>().Property(t => t.Id).HasColumnName("Id").HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
        modelBuilder.Entity<Detail>().Property(t => t.Name).HasColumnName("Name");
        modelBuilder.Entity<Detail>().Property(t => t.Count).HasColumnName("Count");
        modelBuilder.Entity<Detail>().Property(t => t.OwnerId).HasColumnName("OwnerId");
        modelBuilder.Entity<Detail>().HasRequired(c => c.Owner).WithMany(d => d.Details);
    }

    void StudentDetailMapping(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<StudentDetail>().ToTable("StudentDetail");
        modelBuilder.Entity<StudentDetail>().HasKey(t => new { t.Id });
        modelBuilder.Entity<StudentDetail>().Property(t => t.Id).HasColumnName("Id").HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
        modelBuilder.Entity<StudentDetail>().Property(t => t.StudentStuff).HasColumnName("StudentStuff");
        //modelBuilder.Entity<StudentDetail>().Ignore(t => t.Owner);
    }

}

public class MyDbContextInitializer : DropCreateDatabaseIfModelChanges<MyEntities>
{
    protected override void Seed(MyEntities dbContext)
    {
        base.Seed(dbContext);
    }
}

更新:Fluent Mapping:

DataTextField

有人知道如何实现这个目标吗?

0 个答案:

没有答案