Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- 1---
- public class Student
- {
- [Key]
- public int StudentId { get; set; }
- [Display(Name = "Student Name")]
- [Column(TypeName = "nvarchar(100)")]
- public string StudentName { get; set; }
- [Display(Name = "Number in class")]
- [Column(TypeName = "int")]
- public int Number { get; set; }
- [Display(Name = "City")]
- [Column(TypeName = "nvarchar(100)")]
- public string City { get; set; }
- [Display(Name = "Day of yout birth: ")]
- [Column(TypeName = "date")]
- public DateTime BirthDate { get; set; }
- [Display(Name = "Student class")]
- public SchoolClass SchClass { get; set; }
- }
- --- 2 ---
- public class SchoolClass
- {
- [Key]
- public int ClassId { get; set; }
- [Display(Name = "Class name")]
- [Column(TypeName = "nvarchar(20)")]
- public string ClassName { get; set; }
- public List<Student> StudentList { get; set; }
- [NotMapped]
- [Display(Name = "Students count")]
- public int StudentCount { get; }
- }
- ---- 3 ----
- public class ApplicationDBContext : DbContext
- {
- public ApplicationDBContext(DbContextOptions<ApplicationDBContext> options)
- : base(options)
- {
- }
- public DbSet<Student> students { get; set; }
- public DbSet<SchoolClass> classes { get; set; }
- protected override void OnModelCreating(ModelBuilder modelBuilder)
- {
- modelBuilder.Entity<SchoolClass>()
- .HasMany(stcl => stcl.StudentList)
- .WithOne(st => st.SchClass);
- }
- --- 4 ---
- ..... appsetings.json....
- "ConnectionStrings": {
- "SchoolDbStr": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=SchoolDBTest;"
- }
- --- 5 ----
- .... Startup.cs -> void Configure()...
- services.AddDbContext<ApplicationDBContext>(dbContOp => dbContOp.UseSqlServer(Configuration.GetConnectionString("SchoolDbStr")));
- -- 6 ---
- ... Package Manager Console ...
- Add-Migration "ÏnitialCreate"
- Update-Database
- -- 7 --
- ... Custom Validation --
- public class NumberValidation : ValidationAttribute
- {
- private readonly int Max;
- public NumberValidation(int max)
- {
- this.Max = max;
- }
- public override bool IsValid(object value)
- {
- if (!(value is int))
- {
- return false;
- }
- int num = (int)value;
- if (num > Max)
- {
- return false;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement