Results 1 to 2 of 2

Thread: Extending entity classes

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Location
    cobwebbed to PC
    Posts
    311

    Extending entity classes

    Hi Folks

    I'm using LINQ to Entities to interact with my SQL Server DB. I'd like to extend (add partial class bits) the generated partial entity classes and have a list of questions related to that. This is also my first investigation of using partial classes!

    1) So far I have been looking at the code in myedmx.Designer.vb file (created by using the visual edmx creator thing) to see what is already generated for the partial class. Is there generally any other bits of these partials stored in any other files I might be missing?

    2) If one of the auto generated partials is declared as below, in my new class file with the extending partial classes do I need to re-specify:
    - the namespace
    - the classes attributes
    - the inheritance
    Or is this taken care of by the fact of it being a partial of the same class? I.E. will a new partial automatically "share" its sibling's attributes and inheritance?

    VB Code:
    1. '
    2. <EdmEntityTypeAttribute(NamespaceName:="mySqlDataModel", Name:="myTableName")>
    3. <Serializable()>
    4. <DataContractAttribute(IsReference:=True)>
    5. Public Partial Class myTableName
    6.     Inherits EntityObject
    7.     #Region "Factory Method"
    8.     '....
    9.     #End Region
    10.     #Region "Primitive Properties"
    11.     '....
    12.     #End Region
    13. End Class

    3) If I would like to add some business type logic in my new partial class, what is the best way to generically reference the context that the class is part of?
    I.E. If I wanted a function in partial class myTableName to query another table within the same context, is there some way of replacing where we would normally directly reference a context such that a query might be able to reference its parent context?
    E.G.
    VB Code:
    1. '
    2. Dim queryFromOtherTable = From q in Me.ParentContext.OtherTableName ...
    Or is there a better manner entirely of doing such?

    EDIT: Thought I might give a little more info on Q3:

    I have two tables "RackBay" and "Program". I have added a new member "AcProgramId" in my Public Partial Class RackBay.
    When the user wants to assign an AcProgram to the RackBaythey pass an Id integer to the object as the parameter of a Public Sub
    The Sub first checks some other added private members to see if the user is allowed to set the AcProgram at this time.
    Then the Sub needs to A) confirm that the argument the user passed is a valid id - find the record with the same ID on the Program table
    B) confirm the AC column of the found ID record on the Program table is true.
    D) select the value from the ProgramName column of the found ID record in the Program table.

    Normally I would of course be able to use a simple query such as: Dim AcProgramName = From q In myContext.Program Where q.Id = acIdArgument And q.Ac = True Select q.ProgramName.

    But if I was to do this what would go in for "myContext"?

    Or Maybe I would need to use the partial class to set up some kind of navigation property between the two tables instead?
    Last edited by wolf99; Sep 7th, 2013 at 07:10 AM.
    Thanks

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Location
    cobwebbed to PC
    Posts
    311

    Re: Extending entity classes

    OK so with some further searching question 2 has been mostly answered:


    Inheritance: http://stackoverflow.com/questions/4...nd-inheritance
    and
    Attributes: http://www.codeguru.com/csharp/cshar...VBNET-2005.htm

    I'm *guessing* this means as the namespace relation of the generated classes is set via an attribute and the file they are in does not contain any namespace setting for itself, that I do not need to declare the namespace for my new partials??

    EDIT:

    For 1) I also see that there is the generated XML code behind the graphical edmx model. Anything else?
    Last edited by wolf99; Sep 7th, 2013 at 10:34 AM.
    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width