我正在构建一个wiki,其中包含由子粒子组成的文章。文章可以由多个子文章组成,子文章可以附加到多个文章。在连接表中有一个排序顺序,它定义特定文章的子文章的显示。 父文只包含标题(和元数据),没有文本,所有文本都是通过子文章完成的。
虽然这个排序顺序位于加入表中,但是我无法解决这个问题,我无法从查询中访问它。希望有人能指出我正确的方向。
旁白:我在整个MVC / EF世界中都很陌生,即使是c#/ vb和.NET也是我几个月以来在闲暇时间才开展的工作。我有这些课程:
文章:
var Double = React.createClass({
render:function () {
return Add({
x: this.props.value,
y: this.props.value
});
}
});
Subarticle
public class Article : BaseEntity
{
private ICollection<Category> _categories;
private ICollection<ArticleSubarticle> _subarticles;
public string Title { get; set; }
public int AuthorId { get; set; }
public DateTime CreationDate { get; set; }
public DateTime ?PublishDate { get; set; }
public DateTime ?ChangeDate { get; set; }
public bool Published { get; set; }
public virtual ICollection<Category> Categories
{
get { return _categories ?? (_categories = new List<Category>()); }
protected set { _categories = value; }
}
public virtual ICollection<ArticleSubarticle> Subarticles
{
get { return _subarticles ?? (_subarticles = new List<ArticleSubarticle>()); }
protected set { _subarticles = value; }
}
}
Jointable:
public class Subarticle : Article
{
private ICollection<Attachment> _attachments;
public string ArticleText { get; set; }
public int OriginalArticle { get; set; }
public bool Active { get; set; }
public virtual ICollection<Attachment> Attachments
{
get { return _attachments ?? (_attachments = new List<Attachment>()); }
protected set { _attachments = value; }
}
}
它们映射如下:
文章
public class ArticleSubarticle : BaseEntity
{
public int ParentId { get; set; }
public int ChildId { get; set; }
public int SortOrder { get; set; }
public virtual Article Parent { get; set; }
public virtual Subarticle Child { get; set; }
}
Subarticle
public ArticleMap () {
ToTable("Wiki_Article");
HasKey(a => a.Id);
Property(a => a.Title).HasColumnType("VARCHAR").HasMaxLength(250);
Property(a => a.AuthorId);
Property(a => a.PublishDate).IsOptional();
Property(a => a.ChangeDate).IsOptional();
HasMany(a => a.Categories)
.WithMany()
.Map(a => a.ToTable("Wiki_Article_Category_Mapping"));
}
Jointable
public SubarticleMap()
{
ToTable("Wiki_Subarticle");
HasKey(sa => sa.Id);
Property(a => a.ArticleText)
.IsOptional()
.HasColumnType("TEXT");
Property(a => a.OriginalArticle)
.IsOptional();
HasMany(a => a.Attachments)
.WithMany()
.Map(a => a.ToTable("Wiki_Subarticle_Attachment_Mapping"));
}
这会让我按预期获得数据库。
现在我想要一篇带有子文章的文章,这是由sortorder排序的。 这个查询给我带了子文章的文章,但我似乎无法弄清楚如何在Wiki_Article_Subarticle_Mapping表中找到这个排序顺序。
public ArticleSubarticleMap()
{
ToTable("Wiki_Article_Subarticle_Mapping");
HasKey(asa => new { asa.ParentId, asa.ChildId });
HasRequired(asa => asa.Parent)
.WithMany(asa => asa.Subarticles)
.HasForeignKey(asa => asa.ParentId);
}
有什么想法吗?
提前致谢!
答案 0 :(得分:0)
你的查询当前没有加载子文章,所以我猜他们是懒惰加载的。尝试明确加载它们:
public IList<Article> getArticleByIdWithSortedSubarticles(int ArticleId)
{
var query = _articleRepository.Table;
query = query.Where(a => ArticleId == a.Id)
.Select(a => new { article = a, subs = a.SubArticles.OrderBy(s => s.SortOrder) });
return query.AsEnumerable().Select(m => m.article).ToList();
}