The EntitySpaces Community

Share and learn about the EntitySpaces Architecture.
Welcome to The EntitySpaces Community Sign in | Join | Help
in
Home Forums Photos

Changing Primary Keys

Last post 07-04-2008, 8:26 AM by ndunham. 2 replies.
Sort Posts: Previous Next
  •  07-01-2008, 2:00 PM 10086

    Changing Primary Keys

    I know you don't allow users to change primary keys but permit me to ask this question.

     Let's say I have a theoretical table of recipies like

    RecipeID         int    PK  (Identity Column)
    RecipeName    varchar(35)

    and a directions table, which tells a user what to do and the order in which to do it.

    RecipeID         int    PK (From Parent)
    Order              int    PK
    Activity            varchar(250)

    In the child table the combination of RecipeID and Order makes a very logical key field but the Order may need to change if the user wants to add a new step.  Of course the user could change the Order field to an existing value and create a duplicate but they could not save the record in that state. So are you saying you would create the table like this?

    RecipeID         int    PK
    DirectionId      int   PK  (Identity Column)
    Order              int
    Activity            varchar(250)

    This seems like a waste of a field. SQL management tools have always allowed you to change primary keys and they have those cascase update rules as well.

  •  07-01-2008, 3:36 PM 10088 in reply to 10086

    Re: Changing Primary Keys

    You do not have change the Directions table. You could add a new record with the new Order value, copy over the other fields, mark the original as deleted, and save. If you are working with a collection, then collection.Save() is sufficient. If you are working with two entities, then I would save both inside an esTransactionScope.

    This is the same approach as would be taken for a ManyToMany associative table with a composite key. You Associate the new entity and Dissociate the original. EntitySpaces does an AddNew and MarkAsDeleted, respectively, behind the scenes.


    David Neal Parsons
    www.entityspaces.net
  •  07-04-2008, 8:26 AM 10130 in reply to 10088

    Re: Changing Primary Keys

    I would change the definition of the 'directions' child table (if it's under your control) to the following:

    Recipe Table:
    RecipeID         int    PK  (Identity Column)
    RecipeName    varchar(35)

    Direction Table:
    DirectionID      int    PK (Identity Column, autonumber)
    RecipeID         int    FK (From Parent)
    Order              int    (just a property)
    Activity            varchar(250)

    By changing the Order column to just a property, a simple ES entity will allow you to save order changes without impacting any key(s).

    (2 cents...)


    ES2007.1.12.10.0, VS.NET 2005, SQL2005
View as RSS news feed in XML