Ошибка: ссылка на объект не установлена на экземпляр ошибки объекта
Я пытаюсь вставить некоторые строки в SQL Server 2008 из моего веб-приложения, используя Code-First-Migration (Etity Framework). Ошибка Object Reference not set to an instance of an object error
появляется, когда я пытаюсь выполнить Update-Database -Verbose -Force
из консоли диспетчера пакетов.
Вот класс, который я пытаюсь поставить:
[Table("Poste")]
public class PosteModel
{
[Key]
[Required(ErrorMessage = "A remplir, champs obligatoire !")]
[Display(Name = "Nom du poste")]
public string nomPoste { get; set; }
[Required(ErrorMessage = "A remplir, champs obligatoire !")]
[Display(Name = "Nom d'utilisateur")]
public string nomUtilisateur { get; set; }
[Required(ErrorMessage = "A remplir, champs obligatoire !")]
[Display(Name = "Date d'ajout système")]
public DateTime dateAjoutSysteme { get; set; }
[Display(Name = "Liste des applications liées")]
public string adresseIPPoste { get; set; }
[Display(Name = "Numéro du port")]
public int numPort { get; set; }
[Display(Name = "Système d'exploitation")]
public string systemeExploitation { get; set; }
[Display(Name = "Version du système d'exploitation")]
public string versionSystemeExploitaion { get; set; }
[Display(Name = "Alertes déclenchées")]
public ICollection<AlerteModel> alertes { get; set; }
public PosteModel() { }
public PosteModel(string nomPoste, string nomUtilisateur, int numPort, string adresseIPPoste,
string systemeExploitaion, string versionSystemeExploitaion)
{
this.nomPoste = nomPoste.Trim();
this.nomUtilisateur = nomUtilisateur.Trim();
this.dateAjoutSysteme = DateTime.Now;
this.numPort = numPort;
this.adresseIPPoste = adresseIPPoste.Trim();
this.systemeExploitation = systemeExploitation.Trim();
this.versionSystemeExploitaion = versionSystemeExploitaion.Trim();
this.alertes = null;
}
}
И мой миграционный код:
protected override void Seed(MonitoringN.Models.MonitoringNDataContext context)
{
context.Postes.AddOrUpdate(r => r.nomPoste,
new PosteModel("Siège2-Etage1-Bur18", "Foulen Ben Foulen Weld Foulen", 5409, "192.168.254.3", "Windows 7", "SP1"),
new PosteModel("Siège2-Etage1-Bur28", "Foulen Ben Foulen Weld Falten", 5409, "192.168.254.4", "Windows 7", "SP1")
);
}
Любая блестящая идея, пожалуйста?
1 ответ
Решение
Вы упомянули
public DateTime dateAjoutSysteme { get; set; }
как требуется.
Попробуйте добавить свой класс, как это
var postmodel = new PosteModel
{
nomPoste = "abc",//or whatever name you want
nomUtilisateur = "xyz"//or whatever name you want
....//initialize other fields here
};
а затем добавить его в базу данных, как это
context.PosteModel.Add(postmodel);
убедитесь, что вы не пропустите обязательные атрибуты