EF Core Entity Circular Reference

天涯浪子 提交于 2019-12-11 04:06:26

问题


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:


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

    services.AddMvc()
    .AddJsonOptions(options => {
        options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
    });



回答2:


You should have ignored Competitions collection in user model while building.

 modelBuilder.Entity<User>()
            .Ignore(b => b.Competitions);


来源:https://stackoverflow.com/questions/52240008/ef-core-entity-circular-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!