Spaces:
Running
Running
using Microsoft.EntityFrameworkCore; | |
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel.DataAnnotations; | |
using System.ComponentModel.DataAnnotations.Schema; | |
public class QuoteDbContext : DbContext | |
{ | |
public DbSet<Quote> Quotes { get; set; } | |
// Constructor | |
public QuoteDbContext(DbContextOptions<QuoteDbContext> options) : base(options) | |
{ | |
} | |
protected override void OnModelCreating(ModelBuilder modelBuilder) | |
{ | |
base.OnModelCreating(modelBuilder); | |
// Configure the Quote entity using Fluent API | |
modelBuilder.Entity<Quote>(entity => | |
{ | |
entity.HasKey(q => q.Id); // Primary key | |
// Configure indexed columns | |
entity.HasIndex(q => q.Author); | |
entity.HasIndex(q => q.QuoteText).IsUnique(); | |
// Column length and constraints are defined using Data Annotations, | |
// but Fluent API can also be used if needed. | |
entity.Property(q => q.Author).IsRequired().HasMaxLength(255); | |
entity.Property(q => q.QuoteText).IsRequired().HasMaxLength(1000); | |
entity.Property(q => q.Source).HasMaxLength(255); | |
entity.Property(q => q.Book).HasMaxLength(255); | |
entity.Property(q => q.Categories).HasMaxLength(255); | |
entity.Property(q => q.Url).HasMaxLength(500); | |
entity.Property(q => q.Isbn).HasMaxLength(13); | |
entity.Property(q => q.Language).HasMaxLength(50); | |
entity.Property(q => q.OriginalLanguage).HasMaxLength(50); | |
}); | |
} | |
} | |
public class Quote | |
{ | |
public int Id { get; set; } // Primary key with auto-increment | |
[ | ]|
[ | ]|
public string Author { get; set; } // Indexed column | |
[ | ]|
[ | ]|
[ | ]|
public string QuoteText { get; set; } // Indexed and unique column | |
[ | ]|
public string? Source { get; set; } // Nullable column | |
[ | ]|
public string DataSet { get; set; } | |
[ | ]|
public string? Book { get; set; } // Nullable column | |
[ | ]|
public string? Categories { get; set; } // Nullable column | |
[ | ]|
public string? Url { get; set; } // Nullable column | |
[ | ]|
public string? Isbn { get; set; } // Nullable column | |
[ | ]|
public string? Language { get; set; } // Nullable column | |
[ | ]|
[ | ]|
public string? OriginalLanguage { get; set; } // Nullable column | |
[ | ]|
public DateTime DateCreated { get; set; } = DateTime.Now; // Default to current datetime | |
} |