Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
[C#-CodeFirst]多对多插入两次_C#_Database_Entity Framework_Many To Many_Code First - Fatal编程技术网

[C#-CodeFirst]多对多插入两次

[C#-CodeFirst]多对多插入两次,c#,database,entity-framework,many-to-many,code-first,C#,Database,Entity Framework,Many To Many,Code First,首先,为普通英语和stackoverflow的误用道歉 我正在处理一个类似餐厅的应用程序,我必须处理: -侍者 -碟 -点菜,上面有一份菜品清单和一位负责的服务员 -有订单的表格 问题是,每当我添加新命令时,它都会重新插入碟子,即使它们的ID已经存在:( 这里是主要的代码,其中“plats”表示菜肴,“Servers”表示“服务员”,“commande”表示“订单” public void ouvrirFenetreNouvelleCommande(整数x,整数y) { 新命令管理=新命令管理(

首先,为普通英语和stackoverflow的误用道歉

我正在处理一个类似餐厅的应用程序,我必须处理: -侍者 -碟 -点菜,上面有一份菜品清单和一位负责的服务员 -有订单的表格

问题是,每当我添加新命令时,它都会重新插入碟子,即使它们的ID已经存在:(

这里是主要的代码,其中“plats”表示菜肴,“Servers”表示“服务员”,“commande”表示“订单”

public void ouvrirFenetreNouvelleCommande(整数x,整数y)
{
新命令管理=新命令管理(平台、服务){tabX=x,tabY=y};
nouvelleCommande.raiseNewCommandEvent+=新事件处理程序(creerNouvelleCommande);
nouvelleCommande.tabX=x;
nouvelleCommande.tabY=y;
nouvelleCommande.ShowDialog();
}
公共无效Ouvrirrfenetreencaissercommande(整数x,整数y)
{
mettreAJourGrillePrincipale();
}
public void creerNouvelleCommande(对象发送方,命令e)
{
var fenetre=发送方作为命令;
int i=0;
foreach(表中的var项)
{
if(item.TabX==fenetre.TabX&&item.TabY==fenetre.TabY)
{
使用(var db=new RestaurantContext())
{
db.Commandes.Add(e);
foreach(e.ListPlats中的变量项2)
{
Show(String.Format(“ID:{0}-Nom:{1}”,item2.platId,item2.Nom));
}
db.SaveChanges();
表[i].commandeId=e.commandeId;
表[i].Etat=(int)Etats.OCCUPE;
}
mettreAJourGrillePrincipale();
}
i++;
}
}
我的窗口“命令管理”的代码:

使用系统;
使用System.Collections.Generic;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Shapes;
使用System.Linq;
使用System.Collections.ObjectModel;
命名空间RestaurantManagerDeluxe
{
/// 
///Logique d'interaction pour commande_gestion.xaml
/// 
公共部分类命令:窗口
{
公共观测采集平台;
公共列表平台;
公开名单服务人员;
公立双私立学校;
公共事件处理程序raiseNewCommandEvent;
公共int tabX{get;set;}
公共int表{get;set;}
公共命令管理(列表列表平台、列表服务者)
{
初始化组件();
prixTotalPlatsChoisis=0;
platsChoisis=新的可观察集合();
listBoxNouveauPlats.ItemsSource=platsChoisis;
updateRecap();
plats=列表plats;
服务者=列表服务者;
ListBoxPlatsIstant.ItemsSource=plats;
ListBoxResponsible.ItemsSource=serveurs;
}
私有void ajouterNouveauPlat_Click(对象发送方,RoutedEventArgs e)//Appui sur le bouton“Ajouter”
{
如果(ListBoxPlatsIstant.SelectedIndex<0)
返回;
platsChoisis.Add(listboxplatsesistant.SelectedItem作为Plat);
updateRecap();
}
私有void enleverPlat_Click(对象发送者,路由目标)//Appui sur le bouton“Enlever”
{
如果(listBoxNouveauPlats.SelectedIndex<0)
返回;
platsChoisis.RemoveAt(listBoxNouveauPlats.SelectedIndex);
updateRecap();
}
public void updateRecap()//平台数量和整体数量的投降时间
{
prixTotalPlatsChoisis=0;
foreach(平台中的var项目)
{
prixTotalPlatsChoisis+=项目Prix;
}
labelRecap.Content=String.Format(“从0个平台到1个平台的总体”,platsChoisis.Count,prixTotalPlatsChoisis);
}
私有无效BoutonFinalizerCommande_Click(对象发送者,路由目标)//Lors de l'appui sur le bouton“Finalizer la Commande”,ou“Terminer”
{
新命令=新命令();
nouvelleCommande.listplats=新列表();
foreach(platsChoisis中的var项)//关于choisi上的récupère chaque plat dans la liste des plat que l'
{
新公共物品列表平台添加(项目);
Show(String.Format(“id:{0}”,item.platId));
}
nouvelleCommande.Traite=false;//在一次重唱中出现假车
nouvelleCommande.ResponsableId=(listBoxResponsable.SelectedItem作为Serveur);

如果(nouvelleCommande.listplats.Count可能您应该添加以下内容:

 nouvelleCommande.Plat.Attach(item); //Add this
就像这样:

 private void boutonFinaliserCommande_Click(object sender, RoutedEventArgs e) // Lors de l'appui sur le bouton "Finaliser la Commande", ou "Terminer"
{
    Commande nouvelleCommande = new Commande();
    nouvelleCommande.ListePlats = new List<Plat>();

    foreach(var item in platsChoisis) // On récupère chaque plat dans la liste des plats que l'on a choisi
    {
        nouvelleCommande.Plat.Attach(item); //Add this
        nouvelleCommande.ListePlats.Add(item);
        MessageBox.Show(String.Format("id: {0}", item.platId));
    }

    nouvelleCommande.Traite = false; // False car on a pas encore encaissé le paiement
    nouvelleCommande.ResponsableId = (listBoxResponsable.SelectedItem as Serveur).serveurId;

    if(nouvelleCommande.ListePlats.Count <= 1)
    {
        MessageBox.Show("Vous n'avez renseigné aucun plat", "Commande Impossible", MessageBoxButton.OK, MessageBoxImage.Error);
    }
    if(nouvelleCommande.ResponsableId == 0)
    {
        MessageBox.Show("Vous devez choisir un Responsable de table", "Commande Impossible", MessageBoxButton.OK, MessageBoxImage.Error);
        return;
    }

    raiseNewCommandEvent(this, nouvelleCommande); // On envoi la nouvelle commande à la fenêtre principale qui lui affectera la table
    this.Close();
}
private void boutonfinalizercommande\u Click(对象发送者,RoutedEventArgs e)//Lors de l'appui sur le bouton“finalizer la Commande”,ou“Terminer”
{
新命令=新命令();
nouvelleCommande.listplats=新列表();
foreach(platsChoisis中的var项)//关于choisi上的récupère chaque plat dans la liste des plat que l'
{
nouvelleCommande.Plat.Attach(项);//添加此
新公共物品列表平台添加(项目);
Show(String.Format(“id:{0}”,item.platId));
}
nouvelleCommande.Traite=false;//在一次重唱中出现假车
nouvelleCommande.ResponsableId=(listBoxResponsable.SelectedItem作为Serveur);
if(nouvelleCommande.listplats.Count
 nouvelleCommande.Plat.Attach(item); //Add this
 private void boutonFinaliserCommande_Click(object sender, RoutedEventArgs e) // Lors de l'appui sur le bouton "Finaliser la Commande", ou "Terminer"
{
    Commande nouvelleCommande = new Commande();
    nouvelleCommande.ListePlats = new List<Plat>();

    foreach(var item in platsChoisis) // On récupère chaque plat dans la liste des plats que l'on a choisi
    {
        nouvelleCommande.Plat.Attach(item); //Add this
        nouvelleCommande.ListePlats.Add(item);
        MessageBox.Show(String.Format("id: {0}", item.platId));
    }

    nouvelleCommande.Traite = false; // False car on a pas encore encaissé le paiement
    nouvelleCommande.ResponsableId = (listBoxResponsable.SelectedItem as Serveur).serveurId;

    if(nouvelleCommande.ListePlats.Count <= 1)
    {
        MessageBox.Show("Vous n'avez renseigné aucun plat", "Commande Impossible", MessageBoxButton.OK, MessageBoxImage.Error);
    }
    if(nouvelleCommande.ResponsableId == 0)
    {
        MessageBox.Show("Vous devez choisir un Responsable de table", "Commande Impossible", MessageBoxButton.OK, MessageBoxImage.Error);
        return;
    }

    raiseNewCommandEvent(this, nouvelleCommande); // On envoi la nouvelle commande à la fenêtre principale qui lui affectera la table
    this.Close();
}