Abdul Samid

Abdul Samid

  • NA
  • 134
  • 522

code first approach oneToMany relation crud operation EF

Jan 12 2020 4:32 AM
I have a school project with asp.net mvc code first approach, in this two model one for student i and one for student enrollment details model name is admission, have one to many relation. at registration time of new student data is saved using admission model by which data is is inserted in both tables, but i want to insert data in only admission table not in student table when new enrollment i.e. enrollment of old student.
 
Model is As
 
Student Model
  1. public int Id { getset; }  
  2. [Display(Name ="SR Number")]  
  3. [Required(ErrorMessage ="Can't Left Blank")]  
  4. public int SRNumber { getset; }  
  5. [Display(Name ="Name")]  
  6. [MinLength(3,ErrorMessage ="Your{0} must be at least {1} characters long")]  
  7. [MaxLength(40, ErrorMessage = "Your{0} must be no more than {1} characters")]  
  8. public string Name { getset; }  
  9. [Display(Name = "Father")]  
  10. [MinLength(3, ErrorMessage = "Your{0} must be at least {1} characters long")]  
  11. [MaxLength(40, ErrorMessage = "Your{0} must be no more than {1} characters")]  
  12. public string Father { getset; }  
  13. [Display(Name = "Mother")]  
  14. [MinLength(3, ErrorMessage = "Your{0} must be at least {1} characters long")]  
  15. [MaxLength(40, ErrorMessage = "Your{0} must be no more than {1} characters")]  
  16. public string Mother { getset; }  
Enrollment Model
  1. public int Id { getset; }  
  2. [DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}",  
  3. ApplyFormatInEditMode = true)]  
  4. public DateTime? AdmissionDate { getset; }  
  5. public int SrNo { getset; }  
  6. public AdmissionType AdmissionType { getset; }  
  7. public int ClassMasterId { getset; }  
  8. public int SectionMasterId { getset; }  
  9. public ClassMaster ClassMaster { getset; }  
  10. public SectionMaster SectionMaster { getset; }  
  11. public StudentMaster StudentMaster { getset; }  
  12. At New Student Registration Data is Saved in both table  
  13. using (db = new SchoolDbContext())  
  14. {  
  15. db.Admissions.Add(Admission);  
  16. db.SaveChanges();  
  17. }  
But When a old Student is enroll for new class I want To Insert data only in Enrollment.
 
Please Help Me..
 
I HAVE Used ASP.NET MVC5 WITH ENTITY FRAMEWORK CODE FIRST APPROACH

Answers (1)