I am currently working on a small project with code-first: I have to create a movie database and as usual each movie can have more than one genre (m:n). Since genres are constant, I decided to create an enum genre with all genres in it.
And in the Movie
table I have a list of genres (enum). Obviously this went wrong, because you can't store a list of enums in a database.
So I started looking for answers. I came across many solutions, unfortunately none of them really helped me. So I decided to ask this question. I know this may be a duplicate but the other solutions weren't really helpful.
Some of the solutions I've found are Flags
and SmartEnum
.
I tried both but it didn't really work. Could you please take a look at my code and tell me what I did wrong or if there is another way to convert a list of enums.
Movie:
class Serie
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
[MaxLength(255)]
public string Titel { get; set; } = "";
public virtual List<Genres> Genres { get; } = new();
}
Genres:
public sealed class Genres : SmartEnum<Genres>
{
public static readonly Genres Drama = new(name: "Drama", value: 1);
public static readonly Genres Crime = new(name: "Crime", value: 2);
...
private Genres(string name, int value) : base(name, value)
{ }
}
PS: I know I could do it with an extra class but I want to do it with enum.
EF can work with Enums just fine, though I wouldn't consider your example using movie Genres to be a good candidate for an enumeration given new Genres could be added. The use of SmartEntity is just a class wrapper.
Your example of Genres is a Many-to-Many relationship, so looking at the DB side you would have something like:
Genres
- GenreId (PK)
- Name
Series
- SeriesId (PK)
SeriesGenres
- SeriesId (PK, FK)
- GenreId (PK, FK)
Genre is a simple class, so there is honestly no real benefit to wrapping it with a structure class like SmartEnum. At the end of the day you will want EF to treat it like any other entity so that you can query against it effectively. EF Core 5 can accommodate the many-to-many relationship without having to define the SeriesGenre entity, where a Series simply has a collection of Genre, then configured with a HasMany(x => x.Genres).WithMany()
relationship and the configuration of the SeriesGenre table & FKs. EF can take care of the rest behind the scenes.
A better example for an enumeration would be something like a Status where you would want a fixed set of statuses that business rule logic will act upon and would not change unless the system was updated to account for a new status. For example:
///<summary>
/// Enumeration for order statuses. Ensure this matches the OrderStatuses table.
///</summary>
public enum OrderStatus
{
None = 0,
Pending = 1,
Packing = 2,
Review = 3,
Shipped = 4,
Delivered = 100
}
In this case an Order would have a status recorded for any point in time. Business logic would hinge off the current status state. We'd want to store the order record a Status, but still ensure our database has referential integrity so we would have a Statuses table with corresponding OrderStatusIds matching what is in the Enum. The OrderStatusId column in the Orders table can then have a FK constraint on the OrderStatuses table to keep referential integrity in the database. OrderStatus never gets an entity declaration.
The main recommendations I have when doing this are that: