Insert record Many-to-Many - EF Core

Asked

Viewed 32 times

1

Problem:
Error overflow when trying to create record in table with relationships.

Error

when I pass the virtual members with their type instance:
The bank returns saying it cannot find the key 00000000 in the teamId table.

when I pass virtual members as null:
the bank returns saying it cannot find key with null value.

Doubt
How to enter the record without having to get the full user object and the full team object ? Without interfering with .include used when rescuing the record of the Userteam.

Model Userteam responsible for the "many to many" relationship between Applicationuser ("Table: Aspnetusers") and Team.


    public class UserTeam : Entity
    {
        // Empty constructor for EF
        public UserTeam() { }

        public string UserId { get; set; }
        public virtual ApplicationUser Member { get; set; }
        public Guid TeamId { get; set; }
        public virtual Team Team { get; set; }
        public string RoleId { get; set; }
        public virtual ApplicationRole Role { get; set; }
    }

Method responsible for creating a new record in the table Userteam


    public async Task<(bool succeeded, string message, UserTeam data)> AddUserInTeam(UserTeam teamUserRole){

                await _context.UserTeam.AddAsync(teamUserRole);
                await _context.SaveChangesAsync();
                return (true, "", teamUserRole);
            }

The parameter teamUserRole used in the above method was created in the service as follows

        // assinatura do método ...
        _teamRepository.AddUserInTeam(new UserTeam { TeamId = teamId, UserId = user.Id, RoleId = "9083caa8-f847-4f5b-ae7f-b5d60c2059a1" })

       // fechamento do método ...
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.