TPC 继承 1 个基类,继承 2 个子项,计算 1 个类属性,未计算其他类属性

我正在开发一个有两个类和一个基类的项目。基类拥有 Projects 和 Programs 模型之间的所有公共属性。项目通过 ProgramID 列与 Programs 相关联。Projects 有一个 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

}

数据库上下文映射


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”上的声明属性。验证它没有被明确地从模型中排除并且它是一个有效的原始属性。


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


缥缈止盈
浏览 170回答 1
1回答

慕田峪9158850

在采纳了 Rattles 的建议后,我能够让模型按预期工作。通过创建一个抽象的 getter,我能够成功地将它用作我搜索函数的一个属性。通过 getter 只读取具体属性,它使模型更易于使用/维护。然后我只能在 Project 中使用 ProgramID 属性。谢谢拉特的建议!基类public abstract class PMBase{&nbsp; &nbsp; [Key]&nbsp; &nbsp; [DatabaseGenerated(DatabaseGeneratedOption.Identity)]&nbsp; &nbsp; public virtual int ProjectID { get; set; }&nbsp; &nbsp; [NotMapped]&nbsp; &nbsp; public virtual int? PMGroupID { get { return ProgramIDGetter(); } }&nbsp; &nbsp; public abstract int? ProgramIDGetter();//Omitted rest of Model}程序类public class Program : PMBase{&nbsp; &nbsp; public override int? ProgramIDGetter()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return this.ProjectID;&nbsp; &nbsp; }&nbsp; &nbsp; //NOTE: Database column names were from legacy changes.&nbsp; I wish we could change this, but we do not have time.&nbsp; &nbsp; [Column("ProgramID")]&nbsp; &nbsp; public override int ProjectID { get; set; }&nbsp; &nbsp; public virtual ICollection<Project> Projects { get; set; }&nbsp; &nbsp; //Omitted rest of Model}项目类public class Project : PMBase{&nbsp; &nbsp; public override int? ProgramIDGetter()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return this.ProgramID;&nbsp; &nbsp; }&nbsp; &nbsp; [Display(Name = "Program Number & Name")]&nbsp; &nbsp; public override int? ProgramID { get; set; }&nbsp; &nbsp; //Omitted rest of Model}数据库上下文映射modelBuilder.Entity<Project>()&nbsp; .Map(prj =>&nbsp; {&nbsp; &nbsp; prj.MapInheritedProperties();&nbsp; &nbsp; prj.ToTable("Projects");&nbsp; });modelBuilder.Entity<Program>()&nbsp; .Map(pgm =>&nbsp; {&nbsp; &nbsp; pgm.MapInheritedProperties();&nbsp; &nbsp; pgm.ToTable("Programs");&nbsp; });modelBuilder.Entity<Project>()&nbsp; .HasOptional(prj => prj.Program)&nbsp; .WithMany(pgm => pgm.Projects)&nbsp; .HasForeignKey(prj => prj.ProgramID);
打开App,查看更多内容
随时随地看视频慕课网APP