实体框架类类型属性到蛇案例
问题:实体框架类类型属性到蛇案例 我想将RoomLocation类型的Location映射到 Floor -> location_floor, Building -> location_building, Room -> location_room Room.cs public class Room { [Key] public Guid Id { get; set; } public strin
·
问题:实体框架类类型属性到蛇案例
我想将RoomLocation
类型的Location
映射到
Floor -> location_floor,
Building -> location_building,
Room -> location_room
Room.cs
public class Room
{
[Key]
public Guid Id { get; set; }
public string Title { get; set; }
public RoomLocation Location { get; set; }
public DateTime CreationDate { get; set; }
public DateTime ModificationDate { get; set; }
}
public class RoomLocation
{
public int Floor { get; set; }
public int Building { get; set; }
public int Room { get; set; }
}
数据库图
注意:在一个较旧的项目中,我忘记添加builder.HasKey
并且它实际上工作了我查看了日志,Entity Framework 现在将查询转换为user_
,因为我忘记了到底发生了什么,我无法重做这种情况。
我正在使用带有 SnakeCaseNamingConvention 的Npgsql
实体框架。
解答
有关 Entity Framework 6+ 中类似问题的指导,您可以参考此响应此解决方案专门针对 EF-Core
与 Entity Framework 6+ 中提供的 Complex Types 不同,EF Core 具有 Owned Types 的概念,这涵盖了 Complex Types 支持的大多数用例,但允许扩展使用场景,包括导航属性和键。
虽然在 EF Core 中还没有为此实现属性表示法,但您可以轻松地使用 Fluent API 来配置拥有的类型,而无需对您的类进行任何进一步的更改:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Room>().OwnsOne<RoomLocation>(x => x.Location);
base.OnModelCreating(modelBuilder);
}
这将在此处进一步讨论:Using [ComplexType] in Entity Framework Core
更多推荐
已为社区贡献19918条内容
所有评论(0)