Bonjour,
j'aimerais exploiter ce modèle dans une approche Code First.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 public class Client { public int ID { get; set; } public string Nom { get; set; } } public class Compagnie { public int ID { get; set; } public string Nom { get; set; } } public class Contact<T> { public int ID { get; set; } public string Nom { get; set; } public T Origine { get; set; } } public class ContactClient : Contact<Client> { public string Statut { get; set; } } public class ContactCompagnie : Contact<Compagnie> { public string Localisation { get; set; } }Dans cette configuration, EF me génère une table ContactClients et une table ContactCompagnies avec chacune les champs de la classe Contact<T>.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 public class GroupeContext : DbContext { public GroupeContext() : base("name=GroupeContext") { Database.SetInitializer<GroupeContext>(new GroupeContextInitializer()); } protected override void OnModelCreating(DbModelBuilder modelBuilder) { } public DbSet<Client> Clients { get; set; } public DbSet<ContactClient> ContactClients { get; set; } public DbSet<ContactClientVIP> ContactClientVIPs { get; set; } public DbSet<GroupeClient> GroupeClients { get; set; } }
J'aimerais faire en sorte qu'une table Contact soit la pour partager les champs en commun de la classe contact, à l'exception de l'origine. (Ou alors avoir une sorte de discréminant dans la table contact pour typer l'origine à un client ou une compagnie.)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 ContactsClients : ID (PK, int, non NULL) Nom (nvarchar(max), NULL) Statut (nvarchar(max), NULL) Origine_ID (FK, int, NULL) ContactsClients : ID (PK, int, non NULL) Nom (nvarchar(max), NULL) Localisation (nvarchar(max), NULL) Origine_ID (FK, int, NULL)
En gros, j'aimerais obtenir ça :
Et que l'origine du contact au niveau du model reste sur le contact de base.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 Contacts : ID (PK, int, non NULL) Nom (nvarchar(max), NULL) ContactsClients : ID (PK, FK, int, non NULL) Statut (nvarchar(max), NULL) Origine_ID (FK, int, NULL) ContactsClients : ID (PK, FK, int, non NULL) Localisation (nvarchar(max), NULL) Origine_ID (FK, int, NULL)
Est il possible de mapper ce genre d'héritage générique ? Et comment ?
Partager