Question

I'm trying to insert some lines in SQL Server 2008 from my webapp using Code-First-Migration (Etity Framework). The error Object Reference not set to an instance of an object error appears when I try to execute Update-Database -Verbose -Force from Package Manager Console.

Here is the class that I'm trying to put :

[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;
        }
    }

And my Migration code :

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")
                );
}

Any brilliant idea, please ?

Was it helpful?

Solution

You have mentioned

 public DateTime dateAjoutSysteme { get; set; }

as required.

Try to add your class like this

var postmodel = new PosteModel
                   {
                       nomPoste = "abc",//or whatever name you want
                       nomUtilisateur = "xyz"//or whatever name you want
                       ....//initialize other fields here
                   };

and then add it to database like this

context.PosteModel.Add(postmodel);

make sure you don't miss out required attributes

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top