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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:38:08+00:00 2026-05-26T03:38:08+00:00

i have 2 entities each with a relating c# class. I set up a

  • 0

i have 2 entities each with a relating c# class. I set up a navigation property on table A to contain a reference to many items in table B. When i make a new table A class object i need to be able to create the collection of table B objects in table A. How do i set up the navigation property in the table A c# class?

DATAMODEL:
http://bluewolftech.com/mike/mike/datamodel.jpg

  • 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-26T03:38:08+00:00Added an answer on May 26, 2026 at 3:38 am

    Navigation properties are simple in EF. The example below shows how a navigation property would look:

    public class Foo
    {
        public int FooId { get; set; }
        public string SomeProperty { get; set; }
    
        public virtual IEnumerable<Bar> Bars { get; set; }
    }
    

    Where Foo represents tableA and Bar represents tableB. They key word for the navigation property is virtual which enables lazy-loading by default. This is assuming you’re using EF4.1 Code First.

    EDIT

    Off the top of my head, this should be a good starting template for you:

    public class PointOfInterestContext : DbContext
    {
        public IDbSet<PointOfInterest> PointOfInterest { get; set; }
        public IDbSet<POITag> POITag { get; set; }
        public IDbSet<Tag> Tag { get; set; }
    
        public override OnModelCreating(DbModelBuilder modelBuilder)
        {
            // custom mappings go here
            base.OnModelCreating(modelBuilder)
        }
    }
    
    public class PointOfInterest
    {
        // properties
        public int Id { get; set; }
        public string Title { get; set; }
        // etc...
    
        // navigation properties
        public virtual IEnumerable<POITag> POITags { get; set; }    
    }
    
    public class POITag
    {
        // properties
        public int Id { get; set;}
        public int PointOfInterestId { get; set; }
        public int TagId { get; set; }
    
        // navigation properties
        public virtual PointOfInterest PointOfInterest { get; set; }
        public virtual Tag Tag { get; set; }
    }
    
    public class Tag
    {
        // properties
        public int Id { get; set; }
        public string TagName { get; set; }
        // etc...
    
        // navigation properties
        public virtual IEnumerable<POITags> POITags { get; set; }    
    }
    

    Then you would implement the other logic in your business objects. The entities are supposed to be lightweight and at most should have data attributes. I prefer to use the fluent mappings through the OnModelCreating though.

    Here are a few good references:
    MSDN – EF 4.1 Code First
    Code First Tutorial

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

Sidebar

Related Questions

I have 2 entities Locations and Items. Many to many relationship. So each item
I have entities Word and Tag . Both have To-Many relationship to each other,
Let's say I have entities A, B, C and each A has many B
I have 3 entities and each has a corresponding table. I have a task
Imagine we have a set of entities each of which has its state: free,
I have near about 100 entities and each entity has custom property of byteArray
I have these entities class Foo{ Set<Bar> bars; } class Bar{ Foo parent; String
I have two entities: products and bundles. Each one has its class. A product
I have two entities: Event and Comment. @Entity @Table(name = events) public class Event
I have 2 entities : User, Address. I have set the relationship each User

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.