EF code first FLUENT: many to many with an extra field in the join

I am creating a wiki that has articles that are made up of subparticles. An article can consist of several subunits, and a subunit can be attached to several articles. The join has a sort order that determines the display of subparticles for a particular article. The parent article consists only of the title (and metadata), no text, all text is executed through subparticles.

This is the sort order in the connection, although where I am stuck with atm I cannot access it from my request. Hopefully someone can point me in the right direction.

Sidenote: I'm brand new all over the MVC / EF world, even C # / vb and .NET is something I only work with in a few months and in my spare time.

I have the following classes:

Article:

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; }
    }

}

      

subentries

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; }
        }

    }

      

Jointable:

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; }

    }

      

They are displayed as follows:

Article

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"));



        }

      

subentries

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"));

        }

      

Jointable

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);

        }

      

This gives me the database as expected.

Now I want an article with my subunits, sorted by sort order. This query will deliver me an article with its subparticles, but I cannot figure out how to achieve this sort order in the Wiki_Article_Subarticle_Mapping table.

public IList<Article> getArticleByIdWithSortedSubarticles(int ArticleId)
        {
            var query = _articleRepository.Table;
            query = query.Where(a => ArticleId == a.Id)
                         .Select(a => a);

            var subarticles = query.ToList();

            return subarticles;

        }

      

Any ideas?

Thanks in advance!

+3


source to share


1 answer


Your request is currently not loading subtitles, so I think they are lazy loaded. Try loading them explicitly like this:



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();
}

      

0


source







All Articles