Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9187637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:49:17+00:00 2026-06-17T19:49:17+00:00

I have the following POCO: [Alias(Posts)] public class Post : IReturn<Post> { [AutoIncrement] [PrimaryKey]

  • 0

I have the following POCO:

[Alias("Posts")]
public class Post : IReturn<Post>
{
    [AutoIncrement]
    [PrimaryKey]
    public int PostId { get; set; }
    public DateTime CreatedDate { get; set; }
    [StringLength(50)]
    public string CreatedBy { get; set; }
    [StringLength(75)]
    public string Title { get; set; }
    public string Body { get; set; }
    public int UpVote { get; set; }
    public int DownVote { get; set; }
    public bool IsPublished { get; set; }

    public List<Comment> Comments { get; set; }
    public List<Tag> Tags { get; set; }
}

It has a FK on my Comment and Tag entities. So I’d like to return those in my response from my service, but it says 'Invalid Column name 'Comments'' and 'Invalid Column name 'Tags'' . How do I see which Comments and Tags are attached to my Post, with ORM Lite? In EF I would simply use Include to lazy load my related table information, whats the equivalent?

Edit

In response to the answers, I’ve done this:

public class PostFull
{
    public Post Post { get; set; }
    public List<Comment> Comments { get; set; }
    public List<Tag> Tags { get; set; }
}

Then in my service, I return this, my entity PostTag is an intersection entity as my Post and Tag entities are a M:M relationship:

var posts = Db.Select<Post>().ToList();
var fullPosts = new List<PostFull>();

posts.ForEach(delegate(Post post)
{
    var postTags = Db.Select<PostTag>(x => x.Where(y => y.PostId == 
    post.PostId)).ToList();

    fullPosts.Add(new PostFull()
    {
        Post = post,
        Tags = Db.Select<Tag>(x => x.Where(y => postTags.Select(z => 
                   z.TagId).Contains(y.TagId))).ToList(),
        Comments = Db.Select<Comment>(x => x.Where(y => y.PostId == 
                       post.PostId)).ToList()
    });
});

return fullPosts;

Not sure whether its a good design pattern or not?

Edit 2

Here are my entities:

[Alias("Tags")]
public class Tag
{
    [AutoIncrement]
    [PrimaryKey]
    public int TagId { get; set; }

    [StringLength(50)]
    public string Name { get; set; }
}

[Alias("Posts")]
public class Post
{
    [AutoIncrement]
    [PrimaryKey]
    public int PostId { get; set; }
    public DateTime CreatedDate { get; set; }
    [StringLength(50)]
    public string CreatedBy { get; set; }
    [StringLength(75)]
    public string Title { get; set; }
    public string Body { get; set; }
}

[Alias("PostTags")]
public class PostTag
{
    [AutoIncrement]
    [PrimaryKey]
    public int PostTagId { get; set; }

    [References(typeof(Post))]
    public int PostId { get; set; }

    [References(typeof(Tag))]
    public int TagId { get; set; }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:49:20+00:00Added an answer on June 17, 2026 at 7:49 pm

    Tables in OrmLite are strictly a 1:1 mapping with the underlying db tables.

    This means all complex type properties are blobbed into a db text field with the property name, they’re never used to auto-map to child relations as you’re expecting to do here.

    Here’s an early answer that shows how you could map many to many relations with OrmLite.

    Try to avoid N+1 queries, remember that every call to Db.x is a remote DB query so you should ideally try to avoid any Database calls in a loop.

    Retrieving Posts by Many to Many Table query

    You can use OrmLite’s support for JOINs to construct a Typed query as you would in normal SQL to query by the Many to Many table and find all posts with the specified Tag:

    Create and Populate Posts with Test Data

    db.CreateTable<Post>();
    db.CreateTable<Tag>();
    db.CreateTable<PostTag>();
    
    var post1Id = db.Insert(new Post { 
        CreatedBy = "gistlyn", Title = "Post 1", Body = "Body 1" }, selectIdentity:true);
    var post2Id = db.Insert(new Post { 
        CreatedBy = "gistlyn", Title = "Post 2", Body = "Body 2" }, selectIdentity:true);
    db.Insert(new Tag { Id = 1, Name = "A" }, 
              new Tag { Id = 2, Name = "B" });
    db.Insert(new PostTag { PostId = post1Id, TagId = 1 }, 
              new PostTag { PostId = post1Id, TagId = 2 });
    db.Insert(new PostTag { PostId = post2Id, TagId = 1 });
    

    Create a SQL Expression Joining all related tables:

    When following OrmLite’s normal naming conventions above, OrmLite can infer the relationship between each table saving you from specifying the JOIN expression for each query, e.g:

    var postsWithTagB = db.Select(db.From<Post>()
                                    .Join<PostTag>()
                                    .Join<PostTag,Tag>()
                                    .Where<Tag>(x => x.Name == "B"));
    postsWithTagB.PrintDump();
    

    Where this Query returns just the first Post for Tag B and both Posts for Tag A.

    You can further explore this stand-alone example online by running it Live on Gistlyn.

    Populating all Posts with Tags and Comments

    If this is a small blog and you want to load all the posts with their related tags and comments e.g. in a home page or RSS feed you can load the entire dataset in memory with 4 queries using Linq2Objects to join them with something like:

    //Only 4 DB calls to read all table data
    var posts = Db.Select<Post>();
    var postTags = Db.Select<PostTag>();
    var tags = Db.Select<Tag>();
    var comments = Db.Select<Comment>();
    
    //using Linq2Objects to stitch the data together
    var fullPosts = posts.ConvertAll(post =>
    {
        var postTagIds = postTags
            .Where(x => x.PostId == post.PostId)
            .Select(x => x.PostTagId).ToList();
    
        return new PostFull {
            Post = post,
            Tags = tags.Where(x => postTagIds.Contains(x.TagId)).ToList(),
            Comments = comments.Where(x => x.PostId == post.PostId).ToList(),
        };
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following POCO's: [PetaPoco.TableName(Customer)] [PetaPoco.PrimaryKey(ID)] public class User { [Required(ErrorMessage = Please
Given the following POCO classes: public class Certification { public int Id { get;
I have the following class: public class Foo { public int Id { get;
Say I have the following POCO classes: public class AuditableModel { public int ID
Say I have the following POCO classes: public class Parent { public int ID
I have the following Entity Framework POCO classes: public class Customer { public int
I have the following POCO classes: public class Location { public int LocationId {
I have the following POCO: Public Class T1 <Required()> <MaxLength(128)> <Key(), Column(Order:=0)> Property K1
I have a class that contains another poco class with simple get set properties:
I have the following enum and POCO class public enum Gender { Male, Female,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.