Office de la formation Professionnelle et de la promotion du travail
DRPS/ISGI LAAYOUNE
Le 25/12/2019
Module M9 : Développement Application Client serveur
TP11:
Entité Framework
Filtrage des clients par ville
Formateur :A BENDAOUD
Soit EDM l’entité data modèle suivante :
Qui fait le Mapping Relationnel Objet vers la base de données suivante :
Les Entités
public partial class Client
{
public int Id { get; set; }
public string nom { get; set; }
public string prenom { get; set; }
public Nullable<System.DateTime> dateNaissance { get; set; }
public Nullable<int> IdVille { get; set; }
Module :Application client serveur TP11 Page 1 sur 3
public virtual Ville Ville { get; set; }
}
Ville:
public partial class Ville
{
public int Id { get; set; }
public string nomV { get; set; }
public virtual ICollection<Client> Clients { get; set; }
}
Le DbContext:
public partial class DbLocationEntities : DbContext
{
public DbLocationEntities()
: base("name=DbLocationEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<Client> Clients { get; set; }
public virtual DbSet<Ville> Villes { get; set; }
}
Soit le formulaire suivant :
Utiliser l’entité Framework pour faire la recherche des clients par ville
Module :Application client serveur TP11 Page 2 sur 3
Correction:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void button1_Click_1(object sender, EventArgs e)
{
using (DbLocationEntities context= new DbLocationEntities())
{ string nomVille = textBoxNomVille.Text;
var clients = from c in context.Clients
where c.Ville.nomV == nomVille
select new
{
Id = c.Id,
Nom = c.nom,
Prenom = c.prenom,
DN = c.dateNaissance,
NomVille = c.Ville.nomV
};
dataGridView1.DataSource = clients.ToList();
labInfo.Text = "Liste des clients de la ville: " + nomVille;
}
}
private void Form3_Load(object sender, EventArgs e)
{
}
}
}
Module :Application client serveur TP11 Page 3 sur 3