Entity Core object query returns only one element when navigation property is loaded actively

Work with Entity Core, creating a simple soothing web API. The problem I am facing is that when I query with a simple context.Users.ToListAsync (), I get a list of all the users in the database, but when loading the one-to-one relationship navigation property (context.Users.Include (u => u.Status) .ToListAsync (), I ALWAYS get only the first item in my database.

Here is my code First Models:

public class User
{        

    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string FullName { get { return LastName + ", " + FirstName; } }
    public string Email { get; set; }
    public string Avatar { get; set; }
    public string GUID { get; set; }
    public string InternalId { get; set; }

    // Foreign keys and navigation properties
    public virtual UserStatus Status { get; set; }
}

public class UserStatus
{
    public int Id { get; set; }
    public string Name { get; set; }

    // Foreign keys and navigation properties
    public virtual List<User> Users { get; set; }
}

Each user must have user status. User status should be a table of possible state states. There will be many users with one type of status. Here are my current mappings:

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {

        // Users
        modelBuilder.Entity<User>()
            .HasAlternateKey(p => p.GUID);

        modelBuilder.Entity<User>()
            .HasAlternateKey(p => p.InternalId);

        modelBuilder.Entity<User>()
            .HasAlternateKey(p => p.Email);

        modelBuilder.Entity<User>()
            .Property(p => p.FirstName)
            .HasMaxLength(40)
            .IsRequired();

        modelBuilder.Entity<User>()
            .Property(p => p.LastName)
            .HasMaxLength(40)
            .IsRequired();

        modelBuilder.Entity<User>()
            .Property(p => p.Email)
            .HasMaxLength(250)
            .IsRequired();

        modelBuilder.Entity<User>()
            .HasOne(p => p.Status)
            .WithMany(s => s.Users)
            .HasForeignKey("StatusId")
            .OnDelete(Microsoft.EntityFrameworkCore.Metadata.DeleteBehavior.Restrict);

        // User Status
        modelBuilder.Entity<UserStatus>()
            .Property(s => s.Name)
            .HasMaxLength(40)
            .IsRequired();

    }

And here is a simple data initialization with which I am testing:

        List<UserStatus> status = new List<UserStatus>();

        status.Add(new UserStatus { Name = "Invited" });
        status.Add(new UserStatus { Name = "Registered" });
        status.Add(new UserStatus { Name = "Verified" });
        status.Add(new UserStatus { Name = "Deactivated" });

        foreach (UserStatus s in status)
        {
            context.UserStatus.Add(s);
        }
        context.SaveChanges();

        List<User> users = new List<Users>();

        users.Add(new User {
            FirstName = "Jack",
            LastName = "Test",
            Email = "JackTest@aol.com",
            GUID = "TEST001",
            InternalId = "00001",
            Status = status[0]
        });
        users.Add(new User
        {
            FirstName = "Jane",
            LastName = "Tester",
            Email = "JaneTester@gmail.com",
            GUID = "TEST002",
            InternalId = "00002",
            Status = status[1]
        });

        foreach (User u in users)
        {
            context.Users.Add(u);
        }
        context.SaveChanges();

-API-, , :

    [HttpGet]
    public async Task<IActionResult> Get()
    {
        var users = await context.Users
            .ToListAsync();
        return new OkObjectResult(users);
    }

. . , :

       [HttpGet]
        public async Task<IActionResult> Get()
        {
            var users = await context.Users
                .Include(u => u.Status)
                .ToListAsync();
            return new OkObjectResult(users);
        }

. :

       [HttpGet]
        public async Task<IActionResult> Get()
        {
            var users = await context.Users
                .Include(u => u.Status)
                .LastAsync();
            return new OkObjectResult(users);
        }

, , , , ToListAsync.

- , ? " "? ym? .

!

+4
1

, ?

Loop Json. , . , . , :

fooobar.com/questions/25196/...

ASP.net Core, .

+1

Source: https://habr.com/ru/post/1659232/


All Articles