Advertisement
vencinachev

ASP-Zacodovka

Jan 22nd, 2021
923
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.65 KB | None | 0 0
  1. --- 1---
  2. public class Student
  3.     {
  4.         [Key]
  5.         public int StudentId { get; set; }
  6.  
  7.         [Display(Name = "Student Name")]
  8.         [Column(TypeName = "nvarchar(100)")]
  9.         public string StudentName { get; set; }
  10.  
  11.         [Display(Name = "Number in class")]
  12.         [Column(TypeName = "int")]
  13.         public int Number { get; set; }
  14.  
  15.         [Display(Name = "City")]
  16.         [Column(TypeName = "nvarchar(100)")]
  17.         public string City { get; set; }
  18.  
  19.         [Display(Name = "Day of yout birth: ")]
  20.         [Column(TypeName = "date")]
  21.         public DateTime BirthDate { get; set; }
  22.  
  23.         [Display(Name = "Student class")]
  24.         public SchoolClass SchClass { get; set; }
  25.  
  26.     }
  27.  
  28. --- 2 ---
  29.  
  30. public class SchoolClass
  31.     {
  32.         [Key]
  33.         public int ClassId { get; set; }
  34.  
  35.         [Display(Name = "Class name")]
  36.         [Column(TypeName = "nvarchar(20)")]
  37.         public string ClassName { get; set; }
  38.  
  39.         public List<Student> StudentList { get; set; }
  40.  
  41.         [NotMapped]
  42.         [Display(Name = "Students count")]
  43.         public int StudentCount { get; }
  44.  
  45.     }
  46.  
  47. ---- 3 ----
  48.  
  49.  public class ApplicationDBContext : DbContext
  50.     {
  51.         public ApplicationDBContext(DbContextOptions<ApplicationDBContext> options)
  52.             : base(options)
  53.         {
  54.         }
  55.  
  56.         public DbSet<Student> students { get; set; }
  57.         public DbSet<SchoolClass> classes { get; set; }
  58.  
  59.         protected override void OnModelCreating(ModelBuilder modelBuilder)
  60.         {
  61.             modelBuilder.Entity<SchoolClass>()
  62.                 .HasMany(stcl => stcl.StudentList)
  63.                 .WithOne(st => st.SchClass);
  64.         }
  65.  
  66. --- 4 ---
  67.  
  68. ..... appsetings.json....
  69. "ConnectionStrings": {
  70.     "SchoolDbStr": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=SchoolDBTest;"
  71.   }
  72.  
  73.  
  74. --- 5 ----
  75.  
  76. .... Startup.cs -> void Configure()...
  77.    services.AddDbContext<ApplicationDBContext>(dbContOp => dbContOp.UseSqlServer(Configuration.GetConnectionString("SchoolDbStr")));
  78.  
  79. -- 6 ---
  80. ... Package Manager Console ...
  81.  
  82. Add-Migration "ÏnitialCreate"
  83. Update-Database
  84.  
  85.  
  86. -- 7 --
  87. ... Custom Validation --
  88.  public class NumberValidation : ValidationAttribute
  89.     {
  90.         private readonly int Max;
  91.  
  92.         public NumberValidation(int max)
  93.         {
  94.             this.Max = max;
  95.         }
  96.         public override bool IsValid(object value)
  97.         {
  98.             if (!(value is int))
  99.             {
  100.                 return false;
  101.             }
  102.             int num = (int)value;
  103.             if (num > Max)
  104.             {
  105.                 return false;
  106.             }
  107.             return true;
  108.         }
  109.     }
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement