When I create a new record of entity Room
I get this error:
Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred
while saving the entity changes. See the inner exception for details.MySqlConnector.MySqlException (0x80004005): Field ‘password’ doesn’t
have a default value
This is because Room
has an owner (Player
entity), which it’s trying to create when trying to create itself. I want to create the room and be able to assign the relationships, they shouldn’t be created.
Here are the entities to understand:
public class Room
{
public int Id { get; init; }
public string? Name { get; set; }
public int LayoutId { get; init; }
public RoomLayout? Layout { get; init; }
public int OwnerId { get; init; }
public Player? Owner { get; init; }
public int MaxUsersAllowed { get; set; }
public string? Description { get; set; }
public bool IsMuted { get; init; }
public RoomSettings? Settings { get; set; }
public RoomPaintSettings? PaintSettings { get; set; }
public RoomChatSettings? ChatSettings { get; set; }
public DateTime CreatedAt { get; init; }
}
Here is how I create a new room
var newRoom = new Room
{
Name = eventParser.Name,
LayoutId = layout.Id,
Layout = layout,
OwnerId = client.Player.Id,
Owner = client.Player,
MaxUsersAllowed = 50,
Description = eventParser.Description,
CreatedAt = DateTime.Now
};
newRoom.Settings = new RoomSettings
{
RoomId = newRoom.Id,
Password = ""
};
newRoom.ChatSettings = new RoomChatSettings
{
RoomId = newRoom.Id
};
newRoom.PaintSettings = new RoomPaintSettings
{
RoomId = newRoom.Id,
};
dbContext.Rooms.Add(newRoom);
roomRepository.AddRoom(newRoom);
client.Player.Rooms.Add(newRoom);
await dbContext.SaveChangesAsync();
sam korbin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.