Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Self referencing loop error #10997

Closed
Phylum123 opened this issue Feb 16, 2018 · 8 comments
Closed

Self referencing loop error #10997

Phylum123 opened this issue Feb 16, 2018 · 8 comments
Labels
closed-no-further-action The issue is closed and no further action is planned.

Comments

@Phylum123
Copy link

Phylum123 commented Feb 16, 2018

Hey guys, I found an issue with JSON, Razor Pages (Core 2) and EF (Core 2). I know all about self referencing loops and usually it can be solved with configuration. I added configuration and it did not work, I am still getting the error. Below you can see my configuration in "Startup.cs":

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            services.AddMvc().AddJsonOptions(options =>
            {
                options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
                options.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
            });

            services.AddDbContext<CapitalMarketsRMBSContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("CapitalMarketsRMBSContext")));

            //services.AddDbContext<CapitalMarketsReportsContext>(options =>
            //    options.UseSqlServer(Configuration.GetConnectionString("CapitalMarketsReportsContext")));

        }

and when I make this simple call, I get the error:

      public void OnGet()
        {
            Pool = _context.Rmbspool.Include(p => p.RmbsCertificateRates).First();
        }

Here are both entities involved in the call:

    public partial class RmbsPool: EntityBase
    {
        //*** Omitted other properties for clarity ***

                /// <summary>
        /// Child RmbsCertificateRates where [RMBSCertificateRate].[PoolId] point to this entity (FK_RMBSCertificateRate_RMBSPool)
        /// </summary>
        public virtual System.Collections.Generic.ICollection<RmbsCertificateRate> RmbsCertificateRates { get; set; } // RMBSCertificateRate.FK_RMBSCertificateRate_RMBSPool

    }

And the "Many" class:

 [Serializable]  
public partial class RmbsCertificateRate: EntityBase
    {
        //*** Omitted other properties for clarity ***
               [Required]
        [Display(Name = "Pool ID")]
		public int PoolId
		{
			get { return _poolId; }
			set { SetProperty(ref _poolId, value); }
		}

        /// <summary>
        /// Parent RmbsPool pointed by [RMBSCertificateRate].([PoolId]) FK_RMBSCertificateRate_RMBSPool)
        /// </summary>
        public virtual RmbsPool RmbsPool { get; set; } // FK_RMBSCertificateRate_RMBSPool
    }
@ralmsdeveloper
Copy link
Contributor

Can you explain here the error you see?

@Phylum123
Copy link
Author

An unhandled exception occurred while processing the request.

JsonSerializationException: Self referencing loop detected with type 'System.Collections.Generic.List`1[RMBSWeb.Main.Models.RmbsCertificateRate]'. Path 'data[0].RmbsPool'.
Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CheckForCircularReference(JsonWriter writer, object value, JsonProperty property, JsonContract contract, JsonContainerContract containerContract, JsonProperty containerProperty)

@ralmsdeveloper
Copy link
Contributor

This does not sound like error from EF Core. But I can be wrong.
Unless you have no information in your table. and the error message is not what you want.
Try swapping First() for FirstOrDefault()

@Phylum123
Copy link
Author

I can see the data load into the variable "Pool", but I guess when it is sending it back to the client via JSON, thats when it occurs.

@ralmsdeveloper
Copy link
Contributor

If you can see the data load ... the wonderful EF Core did its part ✅

@ralmsdeveloper
Copy link
Contributor

Can you attach a project to reproduce this error here?
So I could try to play, and some member of the team too.

@smitpatel
Copy link
Contributor

smitpatel commented Feb 16, 2018

      services.AddMvc().AddJsonOptions(options =>
            {
                options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
                options.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
            });

This is incorrect
ReferenceLoopHandling should be set to ignore and not serialize. See #4646 (comment)

@smitpatel smitpatel added the closed-no-further-action The issue is closed and no further action is planned. label Feb 16, 2018
@Phylum123
Copy link
Author

I tried ignore, and it didn't work

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned.
Projects
None yet
Development

No branches or pull requests

4 participants