2014-02-07 119 views
1

我试图从我的webapp中使用代码优先 - 迁移(Etity Framework)在SQL Server 2008中插入一些行。当我尝试从程序包管理器控制台执行Update-Database -Verbose -Force时出现错误Object Reference not set to an instance of an object error错误:对象引用未设置为对象的实例错误

这里是我试图把类:

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

任何高招吗?

+2

究竟问题出?你从哪里得到错误? –

+1

可能重复[什么是NullReferenceException,我该如何解决它?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-doi-i-fix-it) –

+0

@AndreiV,当我尝试从包管理器控制台执行'Update-Database -Verbose -Force'时。 – user3264174

回答

0

要求你提到

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

确保你不会错过所需的属性

+0

请看看Poste类的构造器实现。 – user3264174

+1

你可以显示'DbContext'类吗? – Cybercop

相关问题