我想将Location
类型的RoomLocation
映射到
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_
,因为我忘记了发生了什么,无法重做。
我正在使用带有Npgsql
和SnakeCaseNamingConvention的实体框架。
,EF Core具有Owned Types的概念,它涵盖了复杂类型支持但允许扩展的大多数用例使用方案,包括导航属性和键。有关Entity Framework 6+中类似问题的指南,您可以参考this response此解决方案专门针对EF-Core,>
代替Entity Framework 6+中提供的复杂类型
虽然在EF Core中尚未为此实现任何属性符号,但是您可以轻松地使用Fluent API来配置拥有的类型,而无需对类进行任何进一步的更改:
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Room>().OwnsOne<RoomLocation>(x => x.Location); base.OnModelCreating(modelBuilder); }
这将在SO上进一步讨论:Using [ComplexType] in Entity Framework Core
以下指南适用于Entity Framework