IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Entity Framework Discussion :

EF 4.3 Code First Entite<T> [Débutant]


Sujet :

Entity Framework

  1. #1
    Membre habitué
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2010
    Messages
    95
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Février 2010
    Messages : 95
    Points : 131
    Points
    131
    Par défaut EF 4.3 Code First Entite<T>
    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; }
    }
    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; }
        }
    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
     
    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)
    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.)

    En gros, j'aimerais obtenir ça :

    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)
    Et que l'origine du contact au niveau du model reste sur le contact de base.

    Est il possible de mapper ce genre d'héritage générique ? Et comment ?

  2. #2
    Membre du Club Avatar de k4st0r42
    Homme Profil pro
    Artisan numérique
    Inscrit en
    Janvier 2012
    Messages
    48
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loire (Rhône Alpes)

    Informations professionnelles :
    Activité : Artisan numérique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2012
    Messages : 48
    Points : 68
    Points
    68
    Par défaut
    Tu as 3 types d'héritage en code first :
    - Par hiérarchie : http://weblogs.asp.net/manavi/archiv...archy-tph.aspx
    - Par type : http://weblogs.asp.net/manavi/archiv...-type-tpt.aspx
    - Par type concret : http://weblogs.asp.net/manavi/archiv...uidelines.aspx

    Il me semble que celui qui t'intéresse, c'est le premier.
    Le fossé séparant théorie et pratique est moins large en théorie qu’il ne l’est en pratique.

  3. #3
    Membre habitué
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2010
    Messages
    95
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Février 2010
    Messages : 95
    Points : 131
    Points
    131
    Par défaut
    C'est plus compliqué que cela.

    Le fait que ContactClient hérite de Contact<Client> et que ContactCompagnie hérite de Contact<Compagnie> fait qu'il ne font pas partie de la même hiérarchie. Entity ne peut pas leur faire partager simplement une même table.

    Ici, il génère une table pour ContactClient et ContactCompagnie car il fonctionne par défaut en mode Table par hiérarchie et que les deux objets font partie d'une hiérarchie distinct.

    Pourtant ces deux objets partagent tout de même un tronc commun et je voudrais l'exploiter dans une même table pour éviter la multiplicité des liaisons qui pourrait être associé. Mais l'origine pointe soit sur un client, soit sur une compagnie, il faudrait donc une sorte de discréminant au niveau de la table contact ou mapper l'origine au niveau des tables ContactClients et ContactCompagnies.

    Si quelqu'un s'est déjà posé la question sur ce sujet, je serais ravis de l'entendre.

    Solution alternative à mon problême :
    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
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
     
    public class Contact
        {
            public int ID { get; set; }
            public string Nom { get; set; }
     
            [NotMapped]
            public virtual object Origine { get; set; }
     
        }
     
    [Table("ContactClients")]
        public class ContactClient : Contact
        {
            public string Statut { get; set; }
     
            public Client Client { get; set; }
     
            public override object Origine
            {
                get { return Client; }
                set { Client = (Client)value; }
            }
        }
     
    [Table("ContactCompagnies")]
        public class ContactCompagnie : Contact
        {
            public string Localisation { get; set; }
     
            public Compagnie Compagnie { get; set; }
     
            public override object Origine
            {
                get { return Compagnie; }
                set { Compagnie = (Compagnie)value; }
            }
        }
     
    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<Contact> Contacts { get; set; }
            public DbSet<Compagnie> Compagnies { get; set; }
        }
    Je ne clos pas le topic pour le moment, la question m'intéresse toujours.

  4. #4
    Membre habitué
    Homme Profil pro
    Inscrit en
    Avril 2013
    Messages
    76
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations forums :
    Inscription : Avril 2013
    Messages : 76
    Points : 143
    Points
    143
    Par défaut
    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
        [Table("contact")]
        public abstract class Contact
        {
            public int ID { get; set; }
            public string Nom { get; set; }
            public Contact Origine { get; set; }
        }
        [Table("contactClient")]
        public class Client : Contact
        {
            public string Statut { get; set; }
            public Client Origine { get; set; }
        }
        [Table("contactComp")]
        public class Compagnie : Contact
        {
            public string Localisation { get; set; }
            public Compagnie Origine { get; set; }
        }
     
        public class ModelContext : DbContext
        {
            public ModelContext() : base ("DefaultConnection")
            {
     
            }
            public DbSet<Contact> Contacts { get; set; }
        }
    tu as ainsi l'origine dans la table contact, mais le contact sera un client pour un client et une compagnie pour une compagnie.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 1
    Dernier message: 08/07/2015, 15h05
  2. EF Code First et Read Only Property
    Par redkan dans le forum ASP.NET MVC
    Réponses: 2
    Dernier message: 29/06/2011, 14h22
  3. Réponses: 10
    Dernier message: 17/05/2011, 22h45
  4. [EF Code First] Relations entre tables
    Par john85 dans le forum ASP.NET MVC
    Réponses: 5
    Dernier message: 06/05/2011, 19h12
  5. où insérer le code des entités jpa
    Par cool dans le forum JPA
    Réponses: 4
    Dernier message: 24/04/2008, 17h25

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo