1

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 ?

Cybercop
  • 7,905
  • 19
  • 65
  • 125
user3264174
  • 167
  • 2
  • 3
  • 11
  • 2
    What's the problem exactly? Where do you get the error? – Andrei V Feb 07 '14 at 09:14
  • 1
    possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Sriram Sakthivel Feb 07 '14 at 09:15
  • @AndreiV, when I try to execute `Update-Database -Verbose -Force` from Package Manager Console. – user3264174 Feb 07 '14 at 09:15
  • 5
    Duplicate of [Object Reference Not Set To An Instances of an Object Error](http://stackoverflow.com/questions/16643589/object-reference-not-set-to-an-instance-error) – Suhaib Janjua Feb 07 '14 at 09:25

1 Answers1

0

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

Cybercop
  • 7,905
  • 19
  • 65
  • 125