File size: 2,763 Bytes
d9836ef
 
 
 
a07d469
d9836ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b2ed62
d9836ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a07d469
d9836ef
 
 
 
 
ec947b6
 
 
d9836ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a07d469
d9836ef
 
a07d469
d9836ef
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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.DataSet).IsRequired().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

    [Required]
    [MaxLength(255)]
    public string Author { get; set; }  // Indexed column

    [Required]
    [MaxLength(1000)]
    [Column("quote")]
    public string QuoteText { get; set; }  // Indexed and unique column

    [MaxLength(255)]
    public string? Source { get; set; }  // Nullable column

    [MaxLength(255)]
    public string DataSet { get; set; }

    [MaxLength(255)]
    public string? Book { get; set; }  // Nullable column

    [MaxLength(255)]
    public string? Categories { get; set; }  // Nullable column

    [MaxLength(500)]
    public string? Url { get; set; }  // Nullable column

    [MaxLength(13)]
    public string? Isbn { get; set; }  // Nullable column

    [MaxLength(50)]
    public string? Language { get; set; }  // Nullable column

    [MaxLength(50)]
    [Column("original_language")]
    public string? OriginalLanguage { get; set; }  // Nullable column

    [Column("created_at")]
    public DateTime DateCreated { get; set; } = DateTime.Now;  // Default to current datetime
}