Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 实体框架4.1-将实体分为多个表(代码优先)_Asp.net Mvc 3_Ef Code First - Fatal编程技术网

Asp.net mvc 3 实体框架4.1-将实体分为多个表(代码优先)

Asp.net mvc 3 实体框架4.1-将实体分为多个表(代码优先),asp.net-mvc-3,ef-code-first,Asp.net Mvc 3,Ef Code First,我试图找出如何在我的应用程序中可以“注释”的多个场景中重复使用简单的“注释”实体类型 目前,我有几个用户可以发表评论的实体。例如博客、个人资料和照片——这些都可以被“评论” 我希望能够对这些场景中的每一个使用相同的“Comment”类,但我不希望最终得到一个巨大的表,其中包含所有的注释。我认为至少存储一个BlogComments、PhotoComments和ProfileComments表会更有效率。目前,我的评论类如下所示: public class Comment { [Key]

我试图找出如何在我的应用程序中可以“注释”的多个场景中重复使用简单的“注释”实体类型

目前,我有几个用户可以发表评论的实体。例如博客、个人资料和照片——这些都可以被“评论”

我希望能够对这些场景中的每一个使用相同的“Comment”类,但我不希望最终得到一个巨大的表,其中包含所有的注释。我认为至少存储一个BlogComments、PhotoComments和ProfileComments表会更有效率。目前,我的评论类如下所示:

public class Comment
{
   [Key]
   public int Id { get; set; }

   public int ContextId { get; set; }

   [StringLength(256)]
   public string Content { get; set; }

   public DateTime DatePosted { get; set; }

   public virtual Member Author { get; set; }
}
据推测,我需要“ContextId”字段来引用被评论的特定内容。此Id可能是博客、个人资料或照片的Id。我希望能够像这些类中的普通ICollection一样引用注释,我为照片提供了一些类似的代码作为示例:

public class Photo
{
    [Key]
    public int Id { get; set; }

    [StringLength(48)]
    public string FileName { get; set; }

    public virtual Member Owner { get; set; }

    public virtual ICollection<Comment> Comments { get; set; }
}
公共课照片
{
[关键]
公共int Id{get;set;}
[条次建议修正案(48)]
公共字符串文件名{get;set;}
公共虚拟成员所有者{get;set;}
公共虚拟ICollection注释{get;set;}
}
在我的搜索过程中,我被指向了各种各样的文章,但没有一篇与我的具体情况相关。如何将这些注释集合映射到不同的表,并避免使用注释“超级表”


您可以创建一个抽象的
Comment
类,并从中继承特定的注释,例如
PhotoComment
ProfileComment
。您将能够将注释映射到不同的表

public abstract class Comment
{
   [Key]
   public int Id { get; set; }

   [StringLength(256)]
   public string Content { get; set; }

   public DateTime DatePosted { get; set; }

   public virtual Member Author { get; set; }
}


public class PhotoComment : Comment
{
   public int PhotoId { get; set; }

   public virtual Photo Photo { get; set; }
}


public class Photo
{
    [Key]
    public int Id { get; set; }

    [StringLength(48)]
    public string FileName { get; set; }

    public virtual Member Owner { get; set; }

    public virtual ICollection<PhotoComment> Comments { get; set; }
}
公共抽象类注释
{
[关键]
公共int Id{get;set;}
[StringLength(256)]
公共字符串内容{get;set;}
public DateTime DatePosted{get;set;}
公共虚拟成员作者{get;set;}
}
公共类光电指令:评论
{
公共int PhotoId{get;set;}
公共虚拟照片照片{get;set;}
}
公开课照片
{
[关键]
公共int Id{get;set;}
[条次建议修正案(48)]
公共字符串文件名{get;set;}
公共虚拟成员所有者{get;set;}
公共虚拟ICollection注释{get;set;}
}

您可以创建一个抽象的
Comment
类,并从中继承特定的注释,例如
PhotoComment
ProfileComment
。您将能够将注释映射到不同的表

public abstract class Comment
{
   [Key]
   public int Id { get; set; }

   [StringLength(256)]
   public string Content { get; set; }

   public DateTime DatePosted { get; set; }

   public virtual Member Author { get; set; }
}


public class PhotoComment : Comment
{
   public int PhotoId { get; set; }

   public virtual Photo Photo { get; set; }
}


public class Photo
{
    [Key]
    public int Id { get; set; }

    [StringLength(48)]
    public string FileName { get; set; }

    public virtual Member Owner { get; set; }

    public virtual ICollection<PhotoComment> Comments { get; set; }
}
公共抽象类注释
{
[关键]
公共int Id{get;set;}
[StringLength(256)]
公共字符串内容{get;set;}
public DateTime DatePosted{get;set;}
公共虚拟成员作者{get;set;}
}
公共类光电指令:评论
{
公共int PhotoId{get;set;}
公共虚拟照片照片{get;set;}
}
公开课照片
{
[关键]
公共int Id{get;set;}
[条次建议修正案(48)]
公共字符串文件名{get;set;}
公共虚拟成员所有者{get;set;}
公共虚拟ICollection注释{get;set;}
}

太棒了!这是一个非常好、简单的解决方案。我把问题复杂化了——谢谢:)太棒了!这是一个非常好、简单的解决方案。我把问题复杂化了很多——谢谢:)