Entity Framework Code First - 继承 - 相关项目集合
Entity Framework Code First - Inheritance - Related Items Collections
我正在尝试使用继承在 EF6 中实现模型
我有以下 classes:
基地
评论
页:基础
博文:基础
Page 和 BlogPost 以相同的方式使用评论。所以我将 Base 定义为
public class Base
{
public int ID { get; set; }
// ...
public ICollection<Comment> Comments { get; set; }
}
评论class定义如下:
public class Comment
{
public int ID { get; set; }
public int RelatedItemID { get; set; } // points to Base::ID
public string Text { get; set; }
}
假设我想将数据库设置为 "Table per Type",因此 Page 和 BlogPost 有单独的 tables,每个都有自动递增的 int PK。
现在 EF 知道 table Comment.RelatedItemID 指向哪个了吗?即页面或 BlogPost
无需诉诸 "Table Per Hierarchy" 即可实现此 的最佳方法是什么?
I want to set up the database as "Table per Type", so there are
individual tables for Page and BlogPost each with auto-increment int
PK
有个问题。
描述 看起来也像 TPC, but since you want to use auto-increment primary keys for each descendant, it won't fit TPC, because ultimately you'll get duplicating primary keys in one entity set. Obviously, it isn't TPT,因为 TPT 假设有一个带有自动递增 ID 的 "base" table,而 "derived" tables 有非自增主键,同时是 "base" table.
的外键
在我看来,这些实体在逻辑上是不相关的。我的意思是,在任何情况下,您都希望使用单一查询来查询页面和博客文章。因此,最好避免在 EF 模型中继承。
我建议你这样重新设计模型:
// "abstract" tells EF, that this type doesn't require mapping
public abstract class CommentBase
{
public int ID { get; set; }
public int RelatedItemID { get; set; }
public string Text { get; set; }
}
public class PageComment: CommentBase {}
public class BlogPostComment : CommentBase {}
public abstract Base<TComment>
where TComment : Comment
{
public int ID { get; set; }
// ...
public ICollection<TComment> Comments { get; set; }
}
public class Page : Base<PageComment> { /* other page properties */ }
public class BlogPost : Base<BlogPostComment> { /* other blog post properties */ }
代码上还是有继承,但是EF模型中会有两个不同的实体集。 OTOH,您将获得两个单独的 table 评论 - 一个用于页面,一个用于博客文章。
我正在尝试使用继承在 EF6 中实现模型
我有以下 classes:
基地
评论
页:基础
博文:基础
Page 和 BlogPost 以相同的方式使用评论。所以我将 Base 定义为
public class Base
{
public int ID { get; set; }
// ...
public ICollection<Comment> Comments { get; set; }
}
评论class定义如下:
public class Comment
{
public int ID { get; set; }
public int RelatedItemID { get; set; } // points to Base::ID
public string Text { get; set; }
}
假设我想将数据库设置为 "Table per Type",因此 Page 和 BlogPost 有单独的 tables,每个都有自动递增的 int PK。
现在 EF 知道 table Comment.RelatedItemID 指向哪个了吗?即页面或 BlogPost
无需诉诸 "Table Per Hierarchy" 即可实现此 的最佳方法是什么?
I want to set up the database as "Table per Type", so there are individual tables for Page and BlogPost each with auto-increment int PK
有个问题。
描述 看起来也像 TPC, but since you want to use auto-increment primary keys for each descendant, it won't fit TPC, because ultimately you'll get duplicating primary keys in one entity set. Obviously, it isn't TPT,因为 TPT 假设有一个带有自动递增 ID 的 "base" table,而 "derived" tables 有非自增主键,同时是 "base" table.
在我看来,这些实体在逻辑上是不相关的。我的意思是,在任何情况下,您都希望使用单一查询来查询页面和博客文章。因此,最好避免在 EF 模型中继承。
我建议你这样重新设计模型:
// "abstract" tells EF, that this type doesn't require mapping
public abstract class CommentBase
{
public int ID { get; set; }
public int RelatedItemID { get; set; }
public string Text { get; set; }
}
public class PageComment: CommentBase {}
public class BlogPostComment : CommentBase {}
public abstract Base<TComment>
where TComment : Comment
{
public int ID { get; set; }
// ...
public ICollection<TComment> Comments { get; set; }
}
public class Page : Base<PageComment> { /* other page properties */ }
public class BlogPost : Base<BlogPostComment> { /* other blog post properties */ }
代码上还是有继承,但是EF模型中会有两个不同的实体集。 OTOH,您将获得两个单独的 table 评论 - 一个用于页面,一个用于博客文章。