Self Referencing Loop Detected On Separate Queries [duplicate]












0
















This question already has an answer here:




  • EF Core returns null relations until direct access

    1 answer



  • EF Core Eager Loading nested collections

    1 answer




I'm getting some slightly strange behaviour in an asp.net core web api application, using entity framework to SQL Server.



I have two data entities as follows (simplified somewhat).



public class User
{
public int Id {get;set;}
public string username {get;set;}
public ICollection<Sightings> Sightings { get; set; }
}

public class Sightings
{
public int Id {get;set;}
public string username {get;set;}
public User User {get;set;}
}


Now, this has the potential to cause an infinite loop if loaded incorrectly.



In my code, I added a line as follows:



public IActioNResult Get(string username)
{
var sightings = dbContext.Sightings.Where(x => x.username == username);
return Ok(sightings);
}


If I do the code as above, this works perfectly, and returns only the sightings object. The Users field is null, as I have not asked for this to be included.



However, I like to first look up the username, and return a 404 if the user doesnt exist:



public IActioNResult Get(string username)
{
var user = dbContext.Users.Where(x => x.username == username);
if (user == null)
return NotFound();


var sightings = dbContext.Sightings.Where(x => x.username == username);
return Ok(sightings);
}


Now, as soon as I do this, I start getting a "self referencing loop" error. Why is this occurring? I have not asked for the users table to be included - I've done it as two separate queries.



If I comment out the first query - where I look up the user, it then works fine again. There's something about first looking up the users details that entity framework seems to be caching, and automatically including in the second query, even though I've not asked for it to be included.



Is there a way of disabling this behaviour?










share|improve this question













marked as duplicate by Ivan Stoev entity-framework-core
Users with the  entity-framework-core badge can single-handedly close entity-framework-core questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 19:32


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • This is how EF works. And you have no control over it (except no tracking queries of course). "Self reference loops" are not EF problem, but serializers/mappers, hence should be solved by using serialzer/mapper options for handling such data. Or use DTO/ViewModel classes and projections as mentioned in the answer of the second link marked as duplicate.

    – Ivan Stoev
    Jan 2 at 19:38


















0
















This question already has an answer here:




  • EF Core returns null relations until direct access

    1 answer



  • EF Core Eager Loading nested collections

    1 answer




I'm getting some slightly strange behaviour in an asp.net core web api application, using entity framework to SQL Server.



I have two data entities as follows (simplified somewhat).



public class User
{
public int Id {get;set;}
public string username {get;set;}
public ICollection<Sightings> Sightings { get; set; }
}

public class Sightings
{
public int Id {get;set;}
public string username {get;set;}
public User User {get;set;}
}


Now, this has the potential to cause an infinite loop if loaded incorrectly.



In my code, I added a line as follows:



public IActioNResult Get(string username)
{
var sightings = dbContext.Sightings.Where(x => x.username == username);
return Ok(sightings);
}


If I do the code as above, this works perfectly, and returns only the sightings object. The Users field is null, as I have not asked for this to be included.



However, I like to first look up the username, and return a 404 if the user doesnt exist:



public IActioNResult Get(string username)
{
var user = dbContext.Users.Where(x => x.username == username);
if (user == null)
return NotFound();


var sightings = dbContext.Sightings.Where(x => x.username == username);
return Ok(sightings);
}


Now, as soon as I do this, I start getting a "self referencing loop" error. Why is this occurring? I have not asked for the users table to be included - I've done it as two separate queries.



If I comment out the first query - where I look up the user, it then works fine again. There's something about first looking up the users details that entity framework seems to be caching, and automatically including in the second query, even though I've not asked for it to be included.



Is there a way of disabling this behaviour?










share|improve this question













marked as duplicate by Ivan Stoev entity-framework-core
Users with the  entity-framework-core badge can single-handedly close entity-framework-core questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 19:32


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • This is how EF works. And you have no control over it (except no tracking queries of course). "Self reference loops" are not EF problem, but serializers/mappers, hence should be solved by using serialzer/mapper options for handling such data. Or use DTO/ViewModel classes and projections as mentioned in the answer of the second link marked as duplicate.

    – Ivan Stoev
    Jan 2 at 19:38
















0












0








0









This question already has an answer here:




  • EF Core returns null relations until direct access

    1 answer



  • EF Core Eager Loading nested collections

    1 answer




I'm getting some slightly strange behaviour in an asp.net core web api application, using entity framework to SQL Server.



I have two data entities as follows (simplified somewhat).



public class User
{
public int Id {get;set;}
public string username {get;set;}
public ICollection<Sightings> Sightings { get; set; }
}

public class Sightings
{
public int Id {get;set;}
public string username {get;set;}
public User User {get;set;}
}


Now, this has the potential to cause an infinite loop if loaded incorrectly.



In my code, I added a line as follows:



public IActioNResult Get(string username)
{
var sightings = dbContext.Sightings.Where(x => x.username == username);
return Ok(sightings);
}


If I do the code as above, this works perfectly, and returns only the sightings object. The Users field is null, as I have not asked for this to be included.



However, I like to first look up the username, and return a 404 if the user doesnt exist:



public IActioNResult Get(string username)
{
var user = dbContext.Users.Where(x => x.username == username);
if (user == null)
return NotFound();


var sightings = dbContext.Sightings.Where(x => x.username == username);
return Ok(sightings);
}


Now, as soon as I do this, I start getting a "self referencing loop" error. Why is this occurring? I have not asked for the users table to be included - I've done it as two separate queries.



If I comment out the first query - where I look up the user, it then works fine again. There's something about first looking up the users details that entity framework seems to be caching, and automatically including in the second query, even though I've not asked for it to be included.



Is there a way of disabling this behaviour?










share|improve this question















This question already has an answer here:




  • EF Core returns null relations until direct access

    1 answer



  • EF Core Eager Loading nested collections

    1 answer




I'm getting some slightly strange behaviour in an asp.net core web api application, using entity framework to SQL Server.



I have two data entities as follows (simplified somewhat).



public class User
{
public int Id {get;set;}
public string username {get;set;}
public ICollection<Sightings> Sightings { get; set; }
}

public class Sightings
{
public int Id {get;set;}
public string username {get;set;}
public User User {get;set;}
}


Now, this has the potential to cause an infinite loop if loaded incorrectly.



In my code, I added a line as follows:



public IActioNResult Get(string username)
{
var sightings = dbContext.Sightings.Where(x => x.username == username);
return Ok(sightings);
}


If I do the code as above, this works perfectly, and returns only the sightings object. The Users field is null, as I have not asked for this to be included.



However, I like to first look up the username, and return a 404 if the user doesnt exist:



public IActioNResult Get(string username)
{
var user = dbContext.Users.Where(x => x.username == username);
if (user == null)
return NotFound();


var sightings = dbContext.Sightings.Where(x => x.username == username);
return Ok(sightings);
}


Now, as soon as I do this, I start getting a "self referencing loop" error. Why is this occurring? I have not asked for the users table to be included - I've done it as two separate queries.



If I comment out the first query - where I look up the user, it then works fine again. There's something about first looking up the users details that entity framework seems to be caching, and automatically including in the second query, even though I've not asked for it to be included.



Is there a way of disabling this behaviour?





This question already has an answer here:




  • EF Core returns null relations until direct access

    1 answer



  • EF Core Eager Loading nested collections

    1 answer








entity-framework-core asp.net-core-webapi






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 11:51









Gavin CoatesGavin Coates

90611338




90611338




marked as duplicate by Ivan Stoev entity-framework-core
Users with the  entity-framework-core badge can single-handedly close entity-framework-core questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 19:32


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Ivan Stoev entity-framework-core
Users with the  entity-framework-core badge can single-handedly close entity-framework-core questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 19:32


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • This is how EF works. And you have no control over it (except no tracking queries of course). "Self reference loops" are not EF problem, but serializers/mappers, hence should be solved by using serialzer/mapper options for handling such data. Or use DTO/ViewModel classes and projections as mentioned in the answer of the second link marked as duplicate.

    – Ivan Stoev
    Jan 2 at 19:38





















  • This is how EF works. And you have no control over it (except no tracking queries of course). "Self reference loops" are not EF problem, but serializers/mappers, hence should be solved by using serialzer/mapper options for handling such data. Or use DTO/ViewModel classes and projections as mentioned in the answer of the second link marked as duplicate.

    – Ivan Stoev
    Jan 2 at 19:38



















This is how EF works. And you have no control over it (except no tracking queries of course). "Self reference loops" are not EF problem, but serializers/mappers, hence should be solved by using serialzer/mapper options for handling such data. Or use DTO/ViewModel classes and projections as mentioned in the answer of the second link marked as duplicate.

– Ivan Stoev
Jan 2 at 19:38







This is how EF works. And you have no control over it (except no tracking queries of course). "Self reference loops" are not EF problem, but serializers/mappers, hence should be solved by using serialzer/mapper options for handling such data. Or use DTO/ViewModel classes and projections as mentioned in the answer of the second link marked as duplicate.

– Ivan Stoev
Jan 2 at 19:38














0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules