none
Inherited Class With Primary Key of Base Class and Another Key Any Class RRS feed

  • Question

  • Hey guys.


    I have a base class with only one id among other fields.

    In the inherited class, the key is composed by the key of base class and by the key of another class. I am doing mapping but the inherited table has only one id that references base class.


    The inherited table should have two primary keys, how do I make another foreign key to primary key?

    Im using the Entity Framework 6 Code First.
    • Edited by Williame Rocha Friday, July 25, 2014 2:26 PM Incomplete information
    Friday, July 25, 2014 2:06 PM

Answers

  • >Yes, in the object orientation does not make sense, but in the object relational is perfectly possible.

    No.  The opposite.  This is possible in C# from a pure OO modeling POV, but does not make sense and is not possible in the "object relational" model.

    Again your options are to not use inheritance, or to not map the parent class in the relational model, having it only in the OO model.

    David


    David http://blogs.msdn.com/b/dbrowne/

    Monday, July 28, 2014 4:16 PM

All replies

  • Hello,

    It seems that you are want the TeacherID to also be the primary key:

    public class Person
    
        {
    
            public int ID { get; set; }
    
        }
    
    
        public class Teacher : Person
    
        {
    
            public int TeacherID { get; set; }
    
            public string TeacherName { get; set; }
    
    
        }
    

    It is impossible, you need to put the TeacherID in Person entity class. Entities having the inherited association share the primary key.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, July 28, 2014 7:45 AM
    Moderator
  • Hi, thanks for your answer.

    If i have:

         public class Person

        {

            public int Id { get; set; }

        }


    public class Country
    {
    public int Id { get; set; }
    }

        public class Teacher : Person

        {
            public string TeacherName { get; set; }

    //Mapping Foreign Key and mark it like part of primary key
    [Key, Column(Order = 2)]
            [ForeignKey("Country")]
    public int CountryId { get; set; }
    public Country Country { get; set; }
        }

    Why does not work?

    Monday, July 28, 2014 1:21 PM
  • You can't have two different Teachers who are the same Person.  Inheritance in the database requires a 1-1 relationship between the type and the subtype.

    Can you explain your model a bit more?

    And remember you can always _not map_ the supertype in the database, and Teacher will inherit Person only in the .NET model, not in the database.

    David


    David http://blogs.msdn.com/b/dbrowne/

    Monday, July 28, 2014 2:27 PM
  • I have a base class with id of type Int.

    I have a class B inherited of base class.  Class B has an atribute of type C.  The primary key must be id of base class together with primary key of class C.

    The Entity creates only one primary key that is key of base class and the other key is marked like foreign key where must be primary key too.

    Monday, July 28, 2014 3:46 PM
  • Then why model B as a subtype of the base class A?  That doesn't sound like an inheritance relationship.  Instead B is just a type with a foreign key to C and A, and a compound key of (AID,CID).

    David


    David http://blogs.msdn.com/b/dbrowne/


    Monday, July 28, 2014 3:51 PM
  • Yes, in the object orientation does not make sense, but in the object relational is perfectly possible.

    I am not knowing represent this model. I have another classes that inherit of the same base class without problem because the primary key them is  primary key of base class.



    See how i scored the CountryId column in the above example.

    If anyone has any idea, would be very grateful.

    Thanks.

    Monday, July 28, 2014 4:08 PM
  • >Yes, in the object orientation does not make sense, but in the object relational is perfectly possible.

    No.  The opposite.  This is possible in C# from a pure OO modeling POV, but does not make sense and is not possible in the "object relational" model.

    Again your options are to not use inheritance, or to not map the parent class in the relational model, having it only in the OO model.

    David


    David http://blogs.msdn.com/b/dbrowne/

    Monday, July 28, 2014 4:16 PM
  • Okay, thank you. 



    I will review the options and try to reshape with the best possible option. His answers were useful.

    Monday, July 28, 2014 5:32 PM