Adding a Movie Kind Field
To include TV series and mini-series in your movie database, you'll need to add a new field called "MovieKind." Since we didn't include it during the initial table creation, we'll create a new migration to add it to our database.
Note: Do not modify existing migrations, as they will not run again.
Create a new migration file under Migrations/DefaultDB/DefaultDB_20221114_1825_MovieKind.cs:
using FluentMigrator; namespace MovieTutorial.Migrations.DefaultDB; [DefaultDB, MigrationKey(20221114_1825)] public class DefaultDB_20221114_1825_MovieKind : AutoReversingMigration { public override void Up() { Alter.Table("Movie") .AddColumn("Kind").AsInt32().NotNullable().WithDefaultValue(1); } }
Declare a
MovieKind
enumeration that defines the types of movies. You can create this enumeration in Modules/MovieDB/Movie/MovieKind.cs:namespace MovieTutorial.MovieDB; [EnumKey("MovieDB.MovieKind")] public enum MovieKind { [Description("Film")] Film = 1, [Description("TV Series")] TvSeries = 2, [Description("Mini Series")] MiniSeries = 3 }
Add the
Kind
field to theMovieRow
entity class. Manually add theKind
property to yourMovieRow.cs
after theRuntime
property:[DisplayName("Runtime (mins)")] public int? Runtime { get => fields.Runtime[this]; set => fields.Runtime[this] = value; } [DisplayName("Kind"), NotNull] public MovieKind? Kind { get => fields.Kind[this]; set => fields.Kind[this] = value; }
In the
RowFields
class within the sameMovieRow.cs
file, add theKind
field after theRuntime
field:public class RowFields : RowFieldsBase { // ... public Int32Field Runtime; public EnumField<MovieKind> Kind; }
To make the
Kind
field available in the movie form, you need to modify theMovieForm.cs
file:// ... public class MovieForm { // ... public int Runtime { get; set; } public MovieKind Kind { get; set; } }
To make the
Kind
field available in the movie grid, you need to modify theMovieColumns.cs
file:// ... public class MovieColumns { // ... public int Runtime { get; set; } public MovieKind Kind { get; set; } }
Rebuild your solution to ensure there are no build errors and run your application. Now, in the "Add Movie" dialog, you'll have a dropdown to select the movie kind.
Since "Kind" is a required field, you need to provide a default value in the "Add Movie" dialog to avoid validation errors. Most movies will likely be feature films, so let's set "Film" as the default value for the "Kind" property. To add a default value for the "Kind" property, modify the
MovieRow.cs
file by adding aDefaultValue
attribute like this:[DisplayName("Kind"), NotNull, DefaultValue(MovieKind.Film)] public MovieKind? Kind { get => fields.Kind[this]; set => fields.Kind[this] = value; }
Now, when you open the "Add Movie" dialog, the "Kind" field will be pre-filled with "Film".