Skip to content

Commit

Permalink
Add Director and Review models with migrations for database schema
Browse files Browse the repository at this point in the history
  • Loading branch information
tsviz committed Dec 17, 2024
1 parent 0da4369 commit 92f17c0
Show file tree
Hide file tree
Showing 9 changed files with 846 additions and 6 deletions.
17 changes: 15 additions & 2 deletions src/Data/RazorPagesMovieContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,20 @@ public RazorPagesMovieContext(DbContextOptions<RazorPagesMovieContext> options)

public DbSet<Movie> Movie { get; set; } = default!;
public DbSet<User> Users { get; set; } = default!;
public DbSet<Director> Directors { get; set; } = default!;
public DbSet<Review> Reviews { get; set; } = default!;

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

// Director configuration
modelBuilder.Entity<Director>(entity =>
{
entity.Property(e => e.Name).IsRequired().HasMaxLength(100);
entity.Property(e => e.BirthDate).IsRequired();
});

// User configuration
modelBuilder.Entity<User>(entity =>
{
Expand All @@ -38,8 +47,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.OnDelete(DeleteBehavior.Cascade);

entity.HasIndex(m => m.UserId);
entity.Property(m => m.Timestamp).IsConcurrencyToken();
entity.Property(m => m.Timestamp).HasDefaultValue(new byte[8]); // Set default value for Timestamp
entity.Property(m => m.Timestamp).IsRowVersion(); // Ensure Timestamp is configured as rowversion
});

// Seed data with hashed passwords
Expand Down Expand Up @@ -89,6 +97,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
Timestamp = new byte[8]
});
}

foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
{
relationship.DeleteBehavior = DeleteBehavior.NoAction; // Or DeleteBehavior.Restrict
}
}

private static string HashPassword(string password)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected override void Up(MigrationBuilder migrationBuilder)
Price = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
Rating = table.Column<string>(type: "nvarchar(max)", nullable: false),
UserId = table.Column<int>(type: "int", nullable: true),
Timestamp = table.Column<byte[]>(type: "rowversion", rowVersion: true, nullable: false, defaultValue: new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 })
Timestamp = table.Column<byte[]>(type: "rowversion", rowVersion: true, nullable: false)
},
constraints: table =>
{
Expand Down
308 changes: 308 additions & 0 deletions src/Migrations/20241216215338_AddDirectorTable.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 92f17c0

Please sign in to comment.