简体   繁体   English

实体框架TPH工厂方法

[英]Entity Framework TPH Factory Method

I currently have a MVC5 site with a TPH relationship with classes as follows: 我目前有一个MVC5站点与类的TPH关系,如下所示:

public abstract class product{
    public int productID {get;set;}
}

public class toy : product {
     public virtual List<ChildComment> Comments {get;set;}
     public virtual List<AdultComment> Comments {get;set;}
}

public class tool : product {
     public virtual List<AdultComment> Comments {get;set;}
}

public class ChildComment {

     public CommentID {get;set;}
     public string commentText {get;set;}

     public virtual product Product {get;set;}
}

public class AdultComment {

         public CommentID {get;set;}
         public string commentText {get;set;}

         public virtual product Product {get;set;}
    }

My Issue is when: 1) I am creating a new adult comment in the adult comment controller 2) I use db.Products.find(id) to add a product to the product virtual property of the comment 3) I go to the view of the Product I just added the comment to and see that there are 0 comments (lets say I tried to add a comment to a toy, but remember I didn't cast it as a toy when I added it to the virtual property) 4) When I go to the database, there are 3 key columns in the adultcomment table: one for product, one for toys, and one for tools. 我的问题是:1)我在成人评论控制器中创建一个新的成人评论2)我使用db.Products.find(id)将产品添加到评论的产品虚拟属性3)我去视图我只是添加了评论,并看到有0条评论(假设我试图在玩具中添加评论,但请记住,当我将其添加到虚拟财产时,我没有将其作为玩具投射)4当我进入数据库时​​,成人注释表中有3个关键列:一个用于产品,一个用于玩具,一个用于工具。 The correct id was placed in the product column and the others are null 正确的ID放在产品列中,其他ID为null

Do I have to cast a product as either a toy or tool before adding it to the adultcomment's virtual property? 在将产品添加到adultcomment的虚拟财产之前,我是否必须将其作为玩具或工具投射?

Why are there extra columns in the adultcomment table, is it possible to consolidate to one single id column (since after all, i have one products table in my tph), and should I do so if it is possible? 为什么在adultcomment表中有额外的列,是否可以合并到一个id列(因为毕竟,我的tph中有一个产品表),如果可能的话我应该这样做吗?

Add the foreignKey attribute to new Comment class 将foreignKey属性添加到新的Comment类

 public class Comment
{
    [Key]
    public int CommentID { get; set; }
    public string commentText { get; set; }
    public int productID { get; set; }

    [ForeignKey("productID")]
    public virtual Product Product { get; set; }
}

so AdultComment now looks like this 所以AdultComment现在看起来像这样

public class AdultComment  : Comment
{       

}

you will have to add a unique Identifier when creating a new product despite database auto generating id 尽管数据库自动生成id,但在创建新产品时必须添加唯一的标识符

 using (var context = new YOUR-CONTEXT())
 {
      var toy = new Toy
      {
          productID = 1,   //Unique identifier                 
          AdultComments = new List<AdultComment>()
          {
              new AdultComment { commentText = "Some comment" }
          }
       };
       context.Products.Add(toys);
       context.SaveChanges();
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM