我正在使用 EF Core 8。我尝试创建一行,然后创建两个相关行,但对于
RoomPaintSettings
,它会尝试在 FloorPaint
属性具有默认值时填充它的值。它被标记为非空。
为什么 EF Core 认为它比我更了解并尝试使用 null?
var newRoom = new Room
{
Name = eventParser.Name,
LayoutId = layout.Id,
OwnerId = client.Player.Id,
Description = eventParser.Description,
CreatedAt = DateTime.Now
};
dbContext.Rooms.Add(newRoom);
await dbContext.SaveChangesAsync();
newRoom.Settings = new RoomSettings
{
RoomId = newRoom.Id
};
newRoom.PaintSettings = new RoomPaintSettings
{
RoomId = newRoom.Id,
};
await dbContext.SaveChangesAsync();
实体:
public class RoomPaintSettings
{
public int Id { get; set; }
public Room Room { get; set; }
public int RoomId { get; set; }
public string FloorPaint { get; set; } // this has a default in database
}
public class RoomSettings
{
public int Id { get; set; }
public int RoomId { get; set; }
}
不确定为什么我多次被告知这是不可能的。
这是:
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
这不会在
INSERT
或 UPDATE
语句中包含该属性,而是回退到数据库指定的默认值:)