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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:37:28+00:00 2026-06-12T14:37:28+00:00

Trying this question again because my first attempt was barely coherent :p So I

  • 0

Trying this question again because my first attempt was barely coherent :p

So I am super confused and using Entity Framework Code First

I have a Forest class.

I have a Tree class.

Each Forest can have many Trees

When I was trying to serialize I was getting circular reference

public class Forest
{

    public Guid ID { get; set; }  
    public virtual List<Tree> Trees { get; set; }
}
public class Tree
{
    public Guid ID { get; set; }
    public Guid? ForestId {get;set;}

    [ForeignKey("ForestId")]
    public virtual Forest Forest {get;set;}
 }

Every forest has trees but not every tree is in a forest. I struggle with either errors of Multiplicity when doing

@(Html.Raw(Json.Encode(Model)))

Where the model is a Forest

and if I make ForestId a Guid instead of a Guid? I get Circular Reference errors.

I also tried
protected override void

OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder) 
{ 
  modelBuilder.Entity<Forest>() 
  .HasMany(x => x.Tree) 
  .WithOptional() 
   .HasForeignKey(y => y.ForestId); 
}

Thanks in advance

  • 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-12T14:37:30+00:00Added an answer on June 12, 2026 at 2:37 pm

    Best approach would be you should use DTOs to transfer only the data that you want to the client. The DTOs should have just simple properties so it won’t create a circular reference error. At the moment the Forest has List<Trees> Trees and each Tree within Trees has Forest and that Forest again has List<Trees>

    Or

    You can decorate your attributes with ScriptIgnore for properties that you don’t want the
    Json.Encode to serialize and then that wouldn’t be sent back to the client.

    http://msdn.microsoft.com/en-us/library/system.web.script.serialization.scriptignoreattribute.aspx

    Eg:

    public class Forest
    {    
        public Guid ID { get; set; }  
        public virtual List<Tree> Trees { get; set; }
    }
    public class Tree
    {
        public Guid ID { get; set; }
        public Guid? ForestId {get;set;}
    
        [ForeignKey("ForestId")]
        [ScriptIgnore]
        public virtual Forest Forest {get;set;}
     }
    

    Edit:

    Along with ScriptIgnore you should also remove virtual from Forest and Trees and that would work. I’ve tested it. However, I wouldn’t advise that because virtual keyword is what does the Lazy loading. Hence as I said you need to create DTOs based on these Models and only send in DTO to client.

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

Sidebar

Related Questions

I regret to ask this question again. I am trying to implement autocomplete on
While trying to answer this question I found that the code int* p =
Since this question is back to four votes to close, I'm trying again to
While trying to answer this question I found without () (which invokes C++ most
(Came up with this question in the course of trying to answer this other
I have a job interview tomorrow and I'm trying to answer this question: There
Continuing from this question : When I am trying to do fopen on Windows,
See this question. How to show popup message like in stackoverflow I am trying
I asked this question earlier and am now trying to explore the idea of
hello i have this question i was trying to find a way to call

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.