У меня есть таблицы связывания с несколькими внешними ключами на SQL Сервере. Я использую модель представления для создания, редактирования, удаления в базе данных EF сначала, чтобы я мог использовать атрибуты проверки в случае изменений базы данных. Я использовал аннотацию данных, но сопоставленные столбцы идентификаторов не проверяются.
Теперь, в этой ситуации, где добавить атрибут проверки, чтобы я мог получить правильный ответ для каждого столбца при его публикации?
Я не буду использовать атрибуты проверки и аннотации в сгенерированных моделях из базы данных. Тогда где их добавить и что еще нужно сделать?
Пожалуйста, посмотрите эти классы моделей, сгенерированные из базы данных, а также модель представлений.
public partial class Student
{
public Student()
{
this.Resumes = new HashSet<Resume>();
this.StudentSkills = new HashSet<StudentSkill>();
}
public int StudentId { get; set; }
public string StudentName { get; set; }
public int CountryId { get; set; }
public int CityId { get; set; }
public System.DateTime DateOfBirth { get; set; }
public System.Guid ResumeId { get; set; }
public virtual Country Country { get; set; }
public virtual ICollection<StudentSkill> StudentSkills { get; set; }
}
public partial class Skill
{
public Skill()
{
this.StudentSkills = new HashSet<StudentSkill>();
}
public int SkillId { get; set; }
public string SkillName { get; set; }
public bool IsSelected { get; set; }
public virtual ICollection<StudentSkill> StudentSkills { get; set; }
}
public partial class StudentSkill
{
public int StudentSkillsId { get; set; }
public int SkillId { get; set; }
public int StudentId { get; set; }
public virtual Skill Skill { get; set; }
public virtual Student Student { get; set; }
}
public partial class City
{
public int CityId { get; set; }
public string CityName { get; set; }
public int CountryId { get; set; }
public virtual Country Country { get; set; }
}
public partial class Country
{
public int CountryId { get; set; }
public string CountryName { get; set; }
public virtual ICollection<Student> Students { get; set; }
}
public partial class Resume
{
public System.Guid ResumeId { get; set; }
public string ResumeName { get; set; }
public string ResumeExtention { get; set; }
public int StudentId { get; set; }
public virtual Student Student { get; set; }
}
Класс представлений моделей:
public class StudentViewModel
{
[Required(ErrorMessage ="You must enter your name")]
public string StudentName { get; set; }
[Required(ErrorMessage = "You must select a country")]
public int CountryId { get; set; }
[Required(ErrorMessage = "You must select a country")]
public IEnumerable<Country> Country { get; set; }
[Required(ErrorMessage = "You must select a city")]
public int CityId { get; set; }
[Required(ErrorMessage = "You must select a city")]
public IEnumerable<City> City { get; set; }
[Required(ErrorMessage = "Choose at least one skill")]
public int SkillId { get; set; }
[Required(ErrorMessage = "Choose at least one skill")]
public List<Skill> Skills { get; set; }
[Required(ErrorMessage = "You must upload your resume")]
public List<Resume> Resumes { get; set; }
[Required(ErrorMessage = "You must enter Birth Date")]
public System.DateTime DateOfBirth { get; set; }
}