实体框架类类型属性以蛇案

时间:2020-02-08 21:59:45

标签: asp.net postgresql entity-framework-core npgsql

我想将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; }
}

数据库图

Database diagram

注意:在一个较旧的项目中,我忘了添加一个builder.HasKey,并且它确实有效,我查看了日志,并且Entity Framework现在将查询转换为user_,因为我忘记了我能做到的确切情况”重做情况。

我正在将带有Npgsql的实体框架和SnakeCaseNamingConvention一起使用。

3 个答案:

答案 0 :(得分:1)

以下指南适用于实体框架 EF Core 。仍然是.Net Framework实施的相关指南。


通过使用Complex Types可以简化您在此处询问的EF功能。

  • ComplexType没有键,因此不能独立存在。
  • 它只能作为实体类型或其他复杂类型的属性存在。
  • 它不能参与关联并且不能包含导航属性。
  • 复杂类型属性不能为空。
  • 复杂对象的标量属性可以为空。

通过将RoomLocation标记为复杂类型,EF不会在数据库中创建或映射到单独的表,而是会映射到表中包含以下类型的字段:复杂类型的属性。

您可以使用Fluent APIData Annotations进行此操作,但是,由于您使用的是KeyAttribute,因此本示例继续使用数据注释(也称为 Attribute >表示法

因此,将RoomLocation设为复杂类型:

[ComplexType]
public class RoomLocation
{
    public int Floor { get; set; }
    public int Building { get; set; }
    public int Room { get; set; }
}

数据库字段的默认命名约定意味着此表将使用以下默认DB字段映射:

注意::这是MS SQL Server语法, Postgres 中将使用类似的类型,此处的字段名称对于此讨论很重要。

Id UniqueIdentifier NOT NULL,
Title NVarChar(MAX),
Location_Floor INT NOT NULL,
Location_Building INT NOT NULL,
Location_Room INT NOT NULL,
CreationDate DateTime2 NOT NULL
ModificationDate DateTime2 NOT NULL

您可以通过Fluent API使用约定或特定映射,以将这些字段映射到不同的命名字段。

注意::如果您的字段的大小写略有不同(小写,大写或大写),EF仍会解析此映射,并且仍可对 Postgres 数据库字段起作用像您的示例一样都是小写

item.Location.Floor -> location_floor,
item.Location.Building -> location_building,
item.Location.Room -> location_room

答案 1 :(得分:1)

有关Entity Framework 6+中类似问题的指南,您可以参考this response此解决方案专门针对EF-Core

与Entity Framework 6+中提供的复杂类型不同,EF Core具有 Owned Types 的概念,它涵盖了大多数< em>复杂类型受支持,但允许扩展使用方案,包括导航属性和键。

虽然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

答案 2 :(得分:0)

似乎没有人专门介绍过这一点,但实体框架流畅配置生成器确实可以选择定义自定义表和属性映射。

如果我的数据库中有一个名为 notifications 的表,其中包含一些名为 notification_titledate_createddate_modified 的属性。然后你可以在你的类中映射你的属性,如下所示......

您的模型

public class Notification
{
   public DateTime DateCreated { get; set; }
   public DateTime DateModified { get; set; }
   public string NotificationTitle { get; set; }
}

流畅的配置

public class DataContext : DbContext
{
   protected override void OnModelCreating(ModelBuilder modelBuilder)
   {
      modelBuilder.Entity<Notification>(entity =>
      {
         entity.ToTable("notifications");
         entity.Property(p => p.NotificationTitle).HasColumnName("notification_title");
         entity.Property(p => p.DateCreated).HasColumnName("date_created");
         entity.Property(p => p.DateModified).HasColumnName("date_modified");
      });
   }
}

我希望这对某人有所帮助。 ;)