1 回答
TA贡献1794条经验 获得超7个赞
在采纳了 Rattles 的建议后,我能够让模型按预期工作。
通过创建一个抽象的 getter,我能够成功地将它用作我搜索函数的一个属性。通过 getter 只读取具体属性,它使模型更易于使用/维护。然后我只能在 Project 中使用 ProgramID 属性。谢谢拉特的建议!
基类
public abstract class PMBase
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public virtual int ProjectID { get; set; }
[NotMapped]
public virtual int? PMGroupID { get { return ProgramIDGetter(); } }
public abstract int? ProgramIDGetter();
//Omitted rest of Model
}
程序类
public class Program : PMBase
{
public override int? ProgramIDGetter()
{
return this.ProjectID;
}
//NOTE: Database column names were from legacy changes. I wish we could change this, but we do not have time.
[Column("ProgramID")]
public override int ProjectID { get; set; }
public virtual ICollection<Project> Projects { get; set; }
//Omitted rest of Model
}
项目类
public class Project : PMBase
{
public override int? ProgramIDGetter()
{
return this.ProgramID;
}
[Display(Name = "Program Number & Name")]
public override int? ProgramID { get; set; }
//Omitted rest of Model
}
数据库上下文映射
modelBuilder.Entity<Project>()
.Map(prj =>
{
prj.MapInheritedProperties();
prj.ToTable("Projects");
});
modelBuilder.Entity<Program>()
.Map(pgm =>
{
pgm.MapInheritedProperties();
pgm.ToTable("Programs");
});
modelBuilder.Entity<Project>()
.HasOptional(prj => prj.Program)
.WithMany(pgm => pgm.Projects)
.HasForeignKey(prj => prj.ProgramID);
- 1 回答
- 0 关注
- 169 浏览
添加回答
举报