Self Referencing Loop Detected On Separate Queries [duplicate]
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?
entity-framework-core asp.net-core-webapi
marked as duplicate by Ivan Stoev
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.
add a comment |
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?
entity-framework-core asp.net-core-webapi
marked as duplicate by Ivan Stoev
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
add a comment |
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?
entity-framework-core asp.net-core-webapi
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
entity-framework-core asp.net-core-webapi
asked Jan 2 at 11:51
Gavin CoatesGavin Coates
90611338
90611338
marked as duplicate by Ivan Stoev
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
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
add a comment |
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
add a comment |
0
active
oldest
votes
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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