TPC继承1具有2个子代继承的基类,已计算1个类属性,未计算其他类属性

时间:2018-08-03 18:01:04

标签: c# entity-framework

我正在一个有两个类和一个基类的项目中。基类拥有项目和程序模型之间的所有公共属性。通过ProgramID列将项目与程序相关联。项目具有ProgramID的可编辑字段,以使用户可以选择要与该项目关联的程序(并非所有项目都有一个程序,这就是为什么它可以为空)的原因。

从本质上讲,我希望项目能够读取和写入数据库,而程序只能读取(未设置)。 TPC继承有可能吗?任何建议都欢迎。如果需要更多信息,我很乐意提供。预先感谢您的帮助!

基类

public abstract class PMBase
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public virtual int ProjectID { get; set; }

    public virtual int? ProgramID { get; set; }

    public virtual Program Program { get; set; }

    //Omitted rest of Model
}

程序类

public class Program : PMBase
{
    [Column("PMGroupID")]
    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public override int? ProgramID { get { return ProjectID; } set { value = null; } }

    //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<PMBase> Projects { get; set; }

    //Omitted rest of Model
}

项目类

public class Project : PMBase
{
    [Display(Name = "Program Number & Name")]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public override int? ProgramID { get; set; }

    //Omitted rest of Model
}

dbContext映射

modelBuilder.Entity<Project>()
  .Map(prj =>
  {
    prj.MapInheritedProperties();
    prj.ToTable("Projects");
  });

modelBuilder.Entity<Program>()
  .Map(pgm =>
  {
    pgm.MapInheritedProperties();
    pgm.ToTable("Programs");
  });

modelBuilder.Entity<PMBase>()
  .HasOptional(prj => prj.Program)
  .WithMany(pgm => pgm.Projects)
  .HasForeignKey(prj => prj.ProgramID);

看到错误消息

尝试各种方法时会看到这些错误。看到UNION错误时,我感到很接近,但似乎无法克服。

  

\“ PMGroupID \”列不能被修改,因为它要么是计算列,要么是UNION运算符的结果。

     

重写外键组件“ ProgramID”不是类型“ Project”上的已声明属性。确认尚未将其明确排除在模型之外,并且它是有效的原始属性。

     

无法确定相关操作的有效顺序。可能由于外键约束,模型要求或商店生成的值而存在依赖关系。

1 个答案:

答案 0 :(得分:1)

在接受了Rattles的建议之后,我能够按预期运行模型。

通过创建抽象的吸气剂,我能够成功地将其用作搜索功能的属性。使用吸气剂仅读取具体属性,可以使模型更易于使用/维护。然后,我只能在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
}

dbContext映射

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