none
How to Associate Inherited Class in Designer RRS feed

  • Question

  • I have base class with inherited classes in my DataContext designer
    I want to associate another class to the derrived class

    The derrived class does not contain any property in designer (all are inherited from base class) and thats why I cannot add any association.
    When I add the property to the derrived class manualy, with override modifier, I can put there association, but when I want to insert the new instance into the database, the LINQ said that the property 'Int32 Id' does not exist in the base class definition.

    (All other derrived classes without associations working well, but I will need the association there too later).

    What is the best way to use association to the inherited class in LINQ to SQL designer.

    Thanks a lot.

    Saturday, May 1, 2010 7:56 PM

Answers

  • Hello Miroslav,

     

    Welcome to LINQ to SQL forum!

     

    Since LINQ to SQL only supports single-table mapping, and it is same for the FK column, http://social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/f62678fe-e3a0-413c-826e-fd8871105256/, if the base type has the FK property defined, the inherited type needs to hide the parent type’s FK property. 

     

    So if the base type has the FK property but with no association to other tables, please follow these steps to manually add the association to the inherited type in the designer:

     

    1.      Create the inherited type entity in the designer and generate the inheritance.

    2.      Select Discriminator Property and set the values of Base Class Discriminator Value and Derived Class Discriminator Value in the Inheritance properties window.   Also, please set the Inheritance Default to base type. 

    3.      Add a new FK column property in the inherited type entity. 

    4.      Add a new association on the inherited type to another entity. 

    5.      Since the base type does not have an association to that table, we don’t need to set the current association’s Child Property/Inheritance Modifier to new or override.   

    6.      However, since we have a new FK column property in the inherited type, we need to hide the base type’s properties by setting the Inheritance Modifier of the FK column property to new.

    7.      Also, change the Storage name of the FK column in the inherited type to some other name which is different from the one in the base type.   E.g.  _FKColumnID in base type, we use _NewFKColumnID.   Remember to also modify the Column mapping:

    ========================================================================

    [Column(Storage="_NewFKColumnID")]

            public new int FKColumnID

    {

    }

    ========================================================================

     

    Does it work for you?   If you have any questions, please feel free to let me know.

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, May 3, 2010 3:20 AM
    Moderator

All replies

  • Hello Miroslav,

     

    Welcome to LINQ to SQL forum!

     

    Since LINQ to SQL only supports single-table mapping, and it is same for the FK column, http://social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/f62678fe-e3a0-413c-826e-fd8871105256/, if the base type has the FK property defined, the inherited type needs to hide the parent type’s FK property. 

     

    So if the base type has the FK property but with no association to other tables, please follow these steps to manually add the association to the inherited type in the designer:

     

    1.      Create the inherited type entity in the designer and generate the inheritance.

    2.      Select Discriminator Property and set the values of Base Class Discriminator Value and Derived Class Discriminator Value in the Inheritance properties window.   Also, please set the Inheritance Default to base type. 

    3.      Add a new FK column property in the inherited type entity. 

    4.      Add a new association on the inherited type to another entity. 

    5.      Since the base type does not have an association to that table, we don’t need to set the current association’s Child Property/Inheritance Modifier to new or override.   

    6.      However, since we have a new FK column property in the inherited type, we need to hide the base type’s properties by setting the Inheritance Modifier of the FK column property to new.

    7.      Also, change the Storage name of the FK column in the inherited type to some other name which is different from the one in the base type.   E.g.  _FKColumnID in base type, we use _NewFKColumnID.   Remember to also modify the Column mapping:

    ========================================================================

    [Column(Storage="_NewFKColumnID")]

            public new int FKColumnID

    {

    }

    ========================================================================

     

    Does it work for you?   If you have any questions, please feel free to let me know.

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, May 3, 2010 3:20 AM
    Moderator
  • Hi Miroslav,

     

    I am writing to check the status of the issue on your side.  Would you mind letting me know the result of the suggestions? 

     

    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, May 10, 2010 1:29 AM
    Moderator