数据不能插入两个表mvc 5

时间:2017-11-02 16:26:37

标签: c# asp.net-mvc entity-framework-6

基本上问题是重复的,但我需要解决方案

后端:C# - asp.net MVC5 - EF6.13框架 Visual studuio:2013 SQL SERVER:2008

当我想通过两个类将数据插入数据库时​​,会生成消息:

(An exception of type 'System.NullReferenceException' occurred in BusinessErpMVC.dll but was not handled in user code

Additional information: La référence d'objet n'est pas définie à une instance d'un objet.) 

知道这两个类都有一个懒惰的连接,我现在会告诉你我的calsses:

CommandeClt类

 public partial class CommandeClt
    {
        public CommandeClt()
        {
            this.CommandeClt_Ligne = new HashSet<CommandeClt_Ligne>();
        }

    public int CommandeCltID { get; set; }

     [Display(Name = "Code Cmd")]
    public int Cd_CommandeClt { get; set; }

      [Display(Name = "Ref Cmd")]
    public string Ref_CommandeClt { get; set; }

    [Display(Name = "Date Cmd")]
    [DataType(DataType.Date)]
    public Nullable<System.DateTime> Date_CommandeClt { get; set; }

    [Display(Name = "Societe")]
    public byte Id_Societe { get; set; }

    public string Nom_Societe { get; set; }
    public string Adresse_Societe { get; set; }
    public string CodePostal_Societe { get; set; }

    [Display(Name = "Client")]
    public int Id_CltOrigine { get; set; }
    public  string  Nom_CltOrigine { get; set; }
    public string  Adresse_CltOrigine { get; set; }
    public string CodePostal_CltOrigine { get; set; }
    public Nullable<int> Id_CltLivre { get; set; }
    public string Nom_CltLivre { get; set; }
    public string Adresse_CltLivre { get; set; }
    public string CodePostal_CltLivre { get; set; }
    public Nullable<int> Id_CltFacture { get; set; }
    public string Nom_CltFacture { get; set; }
    public string  Adresse_CltFacture { get; set; }
    public string CodePostal_CltFacture { get; set; }
    public int Id_Devise { get; set; }
    public decimal Taux_Devise { get; set; }
    public Nullable<byte> Id_ModeLivraison { get; set; }
    public Nullable<byte> Id_ModeExpedition { get; set; }
    public Nullable<byte> Id_ModeReglement { get; set; }
    public Nullable<System.DateTime> Date_Livraison { get; set; }
    public Nullable<System.DateTime> Date_Expedition { get; set; }
    public Nullable<decimal> Taux_Remise { get; set; }
    public Nullable<decimal> Montant_Remise { get; set; }
    public Nullable<decimal> Taux_Escompte { get; set; }
    public Nullable<decimal> Montant_Escompte { get; set; }
    public Nullable<decimal> Montant_Net_HT { get; set; }
    public Nullable<decimal> Montant_TVA { get; set; }
    public Nullable<decimal> Montant_TTC { get; set; }
    public Nullable<decimal> Acompte { get; set; }
    public Nullable<decimal> Montant_Net_Apayer { get; set; }
    public Nullable<decimal> Poids_Total { get; set; }
    public Nullable<decimal> Volume_Total { get; set; }
    public Nullable<decimal> Metrage_Total { get; set; }
    public Nullable<bool> IsActual { get; set; }
    public string  Commentaire { get; set; }
    public System.DateTime Date_Creation { get; set; }
    public Nullable<System.DateTime> Date_Update { get; set; }
    public Nullable<System.DateTime> Date_Validation { get; set; }
    public Nullable<int> Id_Creator { get; set; }
    public Nullable<int> Id_Updator { get; set; }
    public Nullable<int> Id_Validator { get; set; }
    public Nullable<byte> Etat_CommandeClt { get; set; }

    public virtual ICollection<CommandeClt_Ligne> CommandeClt_Ligne { get; set; }

     [ForeignKey("Id_Societe")]
    public virtual Societe Societe { get; set; }

     [ForeignKey("Id_CltOrigine")]
     [InverseProperty("Id_ClientOrigine")]
     public virtual Partenaire ClientOrigine { get; set; }

     [ForeignKey("Id_CltLivre")]
     [InverseProperty("Id_CltLivre")]
     public virtual Partenaire CltLivre { get; set; }

     [ForeignKey("Id_CltFacture")]
     [InverseProperty("Id_CltFacture")]
     public virtual Partenaire CltFacture { get; set; }

     [ForeignKey("Id_ModeLivraison")] 
     public virtual ModeLivraison ModeLivraison { get; set; }

}

CommandeClt_Ligne class

public partial class CommandeClt_Ligne
    {
        public int CommandeCltID { get; set; }
        public byte Id_CommandeClt_Ligne { get; set; }
        public int Id_Article { get; set; }
        public string Lib_Article { get; set; }
        public Nullable<decimal> Qte_Demandee { get; set; }
        public Nullable<decimal> Qte_Livree { get; set; }
        public Nullable<decimal> Qte_ALivrer { get; set; }
        public Nullable<decimal> Qte_Poids { get; set; }
        public Nullable<decimal> Qte_Volume { get; set; }
        public Nullable<decimal> Qte_Metrage { get; set; }
        public Nullable<byte> Id_Unite { get; set; }
        public Nullable<decimal> Prix_HT { get; set; }
        public Nullable<decimal> Taux_Remise { get; set; }
        public Nullable<byte> Id_TVA { get; set; }
        public Nullable<decimal> Taux_TVA { get; set; }
        public Nullable<System.DateTime> Date_Livraison { get; set; }
        public Nullable<int> Id_DevisClt { get; set; }
        public Nullable<byte> Id_DevisClt_Ligne { get; set; }
        public Nullable<int> Id_CommandeOuverteClt { get; set; }
        public Nullable<byte> Id_CommandeOuverteClt_Ligne { get; set; }
        public string Commentaire { get; set; }
        public byte Etat_CommandeClt_Ligne { get; set; }

        public virtual CommandeClt CommandeClt { get; set; }
    }

C#代码:

[HttpPost]
        public ActionResult Create(CommandeClt commandeclt)
        {
            try
            {
                ViewBag.SelectedSociete = new SelectList(MetiersRepository.SocieteAll(), "Id_Societe", "Cd_Societe");
                ViewBag.SelectedClient = new SelectList(MetiersRepository.ClientAll(), "Id_Partenaire", "Cd_Partenaire");
                ViewBag.SelectedDevise = new SelectList(MetiersRepository.DeviseAll(), "Id_Devise", "Lib_Devise");
                ViewBag.SelectedModeLivraison = new SelectList(MetiersRepository.ModeLivraisonAll(), "Id_ModeLivraison", "Lib_ModeLivraison");
                ViewBag.SelectedModeExpedition = new SelectList(MetiersRepository.ModeExpeditionAll(), "Id_ModeExpedition", "Lib_ModeExpedition");


                if (ModelState.IsValid)
                {

                dbMng.CommandeClts.Add(commandeclt);
                dbMng.SaveChanges();
                    return RedirectToAction("Index");
                }


            }
            catch (DataException /* dex */)
            {
                //Log the error (uncomment dex variable name after DataException and add a line here to write a log.
                ModelState.AddModelError(string.Empty, "Unable to save changes. Try again, and if the problem persists contact your system administrator.");
            }
            return View(commandeclt);
        }

这是更多解释的图片

图片1:

enter image description here

非常感谢

0 个答案:

没有答案