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 868253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:06:32+00:00 2026-05-15T10:06:32+00:00

I am trying to relate 2 items. I have a table that is simply

  • 0

I am trying to relate 2 items. I have a table that is simply an Id field, and then 2 columns for the Item Id’s to relate. I want it to be a 2 way relationship – that is, if the items appear twice in the table, I only want one relationship connection back.

So, here’s my item:

public class Item
{
   public virtual Guid ItemId {get; set;}

   public virtual string Name {get; set;}

   public virtual IList<Item> RelatedItems {get; set;}
}

The table for relating the items looks like this:

CREATE TABLE RelatedItems
(
   RelatedItemId   uniqueidentifier   DEFAULT(NEWID()) NOT NULL,
   ItemId          uniqueidentifier   NOT NULL,
   RelatedId       uniqueidentifier   NOT NULL,

   CONSTRAINT PK_RelatedItems PRIMARY KEY CLUSTERED (RelatedItemId)
)

What is the best way to map this connection?

  • 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-05-15T10:06:32+00:00Added an answer on May 15, 2026 at 10:06 am

    I had to do it the hard way (read HACK).

    HasManyToMany(x => x.RelatedTo)
                    .Table("RelatedItems")
                    .ParentKeyColumn("ItemId")
                    .ChildKeyColumn("RelatedItemId");
    
    HasManyToMany(x => x.RelatedToMe)
                    .Table("RelatedItems")
                    .ChildKeyColumn("ItemId")
                    .ParentKeyColumn("RelatedItemId");
    

    Then in my class I have a read only collection called RelatedItems which joins the two lists and selects distinct items to return.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few models I'm trying to relate. One model is Item ,
I have a table Item with a number of related items, like so: ID
I am trying to retrieve a column from another related table, and display that
I Have Database that contains 4 tables TABLE TBLCARTITEM (CART_ID, ITEM_ID, PROMOTION_ID, many more
Is there a way to map a many-to-many relationship with properties on each item
I have a series of items that I had in a test instance of
I have an xml output like this: <data> <item-types> <entry id=1 items=5> <category>Frozen</category> </entry>
I have a group of related companies that share items they own with one-another.
I have a QTableWidget with a number of columns that are only checkboxes (and
I am trying to select a single column from a related table. I have

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.