簡體   English   中英

錯誤:對象引用未設置為對象實例錯誤

[英]Error : Object Reference not set to an instance of an object error

我正在嘗試使用Code-First-Migration(Etity Framework)從我的Web應用程序在SQL Server 2008中插入一些行。 當我嘗試從Package Manager控制台執行Update-Database -Verbose -Force ,出現Object Reference not set to an instance of an object error的錯誤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")
                );
}

有什么好主意嗎?

你已經提到

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

確保您不會錯過必填屬性

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM