Why is some records deleted when updating others?

asp.net-core c# entity-framework-core

Question

I have a strange problem, where updating some records in my database automatically deletes others.

Models:

public DbSet<Team> Teams { get; set; }
public DbSet<Member> Members { get; set; }

public class Team
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime CreatedAt { get; set; }
    public int Status { get; set; } // -1 = deleted, 0 = incomplete, 1 = completed (signup)
    public Member Captain { get; set; }
}

public class Member
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public bool Captain { get; set; }
    public int Status { get; set; } // -1 = deleted, 0 = inactive, 1 = active
    public int TeamId { get; set; }

    [ForeignKey("TeamId")]
    public Team Team { get; set; }
}

Teams in the database:

Id      Name      CreatedAt                  Status
1       Team 1    2019-07-10 09:32:12.123    1 
2       Team 2    2019-07-10 11:24:39.456    0

Members in the database:

Id      FirstName    LastName    Captain    Status    TeamId
1       John         Doe         True       1         1
2       Jone         Doe         False      1         1
3       Foo          Bar         True       0         2

In the team signup process a new Team is assigned Status 0, and when payment is received, they are updated with Status 1. When the team signup is completed, the Captain can invite other Members to join the team.

Once a day I do a check for incompleted teams (Status 0), and I set the Team and related Member (Captain) to Status -1:

public async Task<IActionResult> OnGetAsync()
{

    var incompletedTeams = await _context.Teams.Include(x => x.Captain).Where(x => x.Status == 0 && (DateTime.Now - x.CreatedAt).Minutes > 30 && x.Captain.Captain).ToListAsync();

    foreach (var team in incompletedTeams)
    {
        team.Status = -1;
        team.Captain.Status = -1;
    }

    await _context.SaveChangesAsync();

    [...]

}

The strange thing is, that when I save these changes, the Member that is not Captain, is removed from the database. The Team og Member (Captain) status is updated correctly.

How is that even possible - and how to avoid it?

1
0
10/9/2019 4:29:16 PM

Accepted Answer

Here is a simple working demo like below:

1.DbContext:

public class SO1010testContext : DbContext
{
    public SO1010testContext (DbContextOptions<SO1010testContext> options)
        : base(options)
    {
    }
    public DbSet<Team> Teams { get; set; }
    public DbSet<Member> Members { get; set; }
}

2.Model(From your database data,one team contains several members,so you need to change the Member to List<Member> in class Team):

public class Team
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime CreatedAt { get; set; }
    public int Status { get; set; } // -1 = deleted, 0 = incomplete, 1 = completed (signup)
    public List<Member> Captain { get; set; }
}
public class Member
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public bool Captain { get; set; }
    public int Status { get; set; } // -1 = deleted, 0 = inactive, 1 = active
    public int TeamId { get; set; }
    [ForeignKey("TeamId")]
    public Team Team { get; set; }
}

3.Method:

public class IndexModel : PageModel
{
    private readonly SO1010testContext _context;

    public IndexModel(SO1010testContext context)
    {
        _context = context;
    }
    public async Task<IActionResult> OnGetAsync()
    {
        var incompletedTeams =  _context.Teams.Include(x => x.Captain)
            .Where(x => x.Status == 0 && (DateTime.Now - x.CreatedAt).Minutes > 30)
            .ToList();
        foreach (var team in incompletedTeams)
        {
            var member = team.Captain;
            var captain = member.Where(m => m.Captain).ToList();
            foreach (var c in captain)
            {
                team.Status = -1;
                c.Status = -1;
            }

        }
        await _context.SaveChangesAsync();
        return Page();
    }
}

4.Result: enter image description here

0
10/10/2019 6:55:36 AM


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