EF Core Entity Circular Reference

asp.net asp.net-core asp.net-core-mvc entity-framework-core

Question

I am using C#.net and ef core. I have the models below. When I get my list of competitions, I want to only get my related user. However, I am getting the User and all the User's competitions. How can I achieve this? I had to do the following to get my list of competitions to show:

.AddJsonOptions(opt => opt.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore)

public partial class Competition
{
    public int CompetitionId { get; set; }
    public int UserId { get; set; }

    public User User { get; set; }
}

public partial class User
{

    public int UserId { get; set; }
    public string Email { get; set; }
    public string UserName { get; set; }

    public ICollection<Competition> Competitions { get; set; }
}

I have an api that uses entity framework to make calls to my database using the models describe above. The call in my api that causes the circular reference is the following:

    [Produces("application/json")]
    [Route("api/Competitions")]
    public class CompetitionsController : Controller
    {
        private readonly ApplicationDBContext _context;

        public CompetitionsController(ApplicationDBContext  context)
        {
            _context = context;
        }

        // GET: api/Competitions
        [HttpGet]
        public IEnumerable<Competition> GetCompetitions()
        {
            //return _context.Competitions;
            return _context.Competitions
                .Include(u => u.User).ToList();
        }
    }

Below is my onmodelcreating code block in my ApplicationDBContext class:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Competition>(entity =>
        {
            entity.HasKey(e => e.CompetitionId);

            entity.HasOne(d => d.User)
                .WithMany(p => p.Competitions)
                .HasForeignKey(d => d.UserId)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_Competitions_Users");
        });

        modelBuilder.Entity<User>(entity =>
        {
            entity.HasKey(e => e.UserId);

            entity.HasIndex(e => e.UserName)
                .HasName("UC_UserName")
                .IsUnique();

            entity.Property(e => e.Email)
                .HasMaxLength(40)
                .IsUnicode(false);

            entity.Property(e => e.UserName)
                .HasMaxLength(40)
                .IsUnicode(false);
        });
    }
1
2
9/9/2018 1:47:52 AM

Popular Answer

Old question, but in case someone still looking. One solution is:

    services.AddMvc()
    .AddJsonOptions(options => {
        options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
    });
2
10/18/2019 12:22:26 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow