Cant Save data in database by using only one save chages

40 Views Asked by At

my problem is that i want to save all my entities by using only one save chages. how can i achive that?.

here is my entity structure

public class Chat
{
    public int Id { get; set; }
    public ChatType ChatType { get; set; }
    public ICollection<ChatMessage> ChatMessages { get; set; }
    public ICollection<ChatUser> ChatUsers { get; set; }
}

public class Group
{
    public int ChatId { get; set; }
    public Chat Chat { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }   
    public ICollection<GroupUser> GroupUsers { get; set; }
}

public class ChatUser
{
    public int Id { get; set; }
    public int ChatId { get; set; }
    public Chat Chat { get; set; }
    public int UserId { get; set; }
    public User User { get; set; }
}

public class GroupUser
{
    public int GroupId { get; set; }
    public Group Group { get; set; }
    public int ChatUserId { get; set; }
    public ChatUser ChatUser { get; set; }
    public ChatRole ChatRole { get; set; }
}

and here is the code where i create entities and want to save it in database.

public async Task<CreatedGroupResponseDto> CreateGroupAsync(int creatorUserId, CreateGroupRequestDto dto)
    {
        var chat = new Chat()
        {
            ChatType = ChatType.Group
        };

        var group = new Group()
        {
            Name = dto.Name,
            Chat = chat,
        };

        var creatorUser = new GroupUser()
        {
            ChatUser = new ChatUser()
            {
                Chat = chat,
                UserId = creatorUserId
            },
            ChatRole = ChatRole.Admin
        };

        var groupUsers = new List<GroupUser> { creatorUser };
        groupUsers.AddRange(await AddGroupUsersAsync(dto.UserIds));
        group.GroupUsers = groupUsers;

        _groupRepository.Insert(group);

        await _unitOfWork.SaveChangesAsync();

        //SignalGroupCreatedAsync(creatorUserId, group);

        return _mapper.Map<CreatedGroupResponseDto>(group);
    }


private async Task<List<GroupUser>> AddGroupUsersAsync(IEnumerable<int> userIds)
    {
        var groupUsers = new List<GroupUser>();

        var users = await _userRepository.GetAllAsync(userIds);

        foreach (var user in users)
        {
            groupUsers.Add(new GroupUser()
            {
                ChatUser = new ChatUser()
                {
                    User = user
                },
                ChatRole = ChatRole.User,
            });
        }

        return groupUsers;
    }

when i come to _unitOfWork.SaveChangesAsync(); i get this error (insert or update on table "ChatUsers" violates foreign key constraint "FK_ChatUsers_Chats_ChatId"). when i looked to the logs i figured out that the order of insertion of this entities are wrong. the order must be like that: Chat => retrive id for group Group ChatUser GroupUser

0

There are 0 best solutions below