none
Are they any invalid values for keys with store-generated-pattern? RRS feed

  • Question

  • WHAT I HAVE:

    Visual Basic 2010, .NET 4.0, Entity Framework 4.0, SQL Express

    MY QUESTION:

    Suppose I'm creating an entity model, Model-First, and I have an entity called PhoneNumbers, which has a primary key, say PhoneNumberID, of type Int32 and StoreGeneratedPattern value of Identity (or Computed) so that it's auto-generated on insert (or on insert & update). Now let's supposed I have an another entity, SocialContacts, which accesses PhoneNumbers via a navigation property (say, ContactPhoneNumbers) and has an Int32 field called DefaultPhoneNumberID, which has the id # of the "default" phone number. My question is as follows: Are there any values that are never used by Identity (or Computed) fields? If a SocialContact doesn't currently have a default PhoneNumber, it would be useful to set DefaultPhoneNumberID to such an invalid value to indicate that fact. (i.e., If the PhoneNumberID can't be, say, 0, then I could set DefaultPhoneNumberID to 0 until a default phone # is selected. Also, can I have the reference to the default phone # in the form of a navigation property, i.e., DefaultPhoneNumber?)


    Robert Gustafson

    Sunday, January 5, 2014 5:34 AM

Answers

  • Hello,

    >> My question is as follows: Are there any values that are never used by Identity (or Computed) fields?

    The identity field is defined as below:

    IDENTITY [ (seed , increment) ]

    We can see that it has a seed, so if we set it to be IDENTITY (1, 1), then it will start from 1 and increase 1 every time, the value which is smaller than 1 will never be called (i.e. 0 or -1).

    So what you need to do is just to set the DefaultPhoneNumberID to be smaller than the value of the seed.

    >> Also, can I have the reference to the default phone # in the form of a navigation property, i.e., DefaultPhoneNumber?

    Is the DefaultPhoneNumber a foreign key? If it is not, of course, you can.

    If it is a foreign key, what is relationship between the two entities? If it is possible, please share it with us so that we could understand your issue better.

    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, January 6, 2014 9:52 AM
    Moderator