Map string column in Entity Framework to Enum

前端 未结 8 1164
被撕碎了的回忆
被撕碎了的回忆 2021-02-18 13:46

Is there a way to map a string column to an enum in an Entity Model?

I have done this in Hibernate, but can\'t figure it out in EMF.

8条回答
  •  礼貌的吻别
    2021-02-18 14:34

    You can do either:

    Decorate the Enum property in your class as a text column

    [Column(TypeName = "nvarchar(50)")]
    public FileTypes FileType { get; set; }
    

    OR

    in your DatabaseContext class, override the OnModelCreating and add:

    modelBuilder
      .Entity()
      .Property(e => e.FileType)
      .HasConversion(new EnumToStringConverter());
    

提交回复
热议问题