none
Getting duplicate key exception without duplicate key RRS feed

  • Question

  • Regarding Linq to SQL with .SQLEXPRESS and VS2010 on Windows 7, I am trying to add a list of entries to an empty table that I have confirmed programmatically does not have duplicate keys. Some info about the situation:

    Note: The key strings have Unicode characters (English and Chinese mixed).

    1. I call DataContext.CreateDatabase.

    2. Here is the relevent part of table class (I'm not using the designer):

    [Table]
    public class DBDefinitionString : ILanguageString
    {
      private string _ID;
      [Column(Storage = "_ID", DbType = "NVarChar(512) NOT NULL", IsPrimaryKey = true)]
      public string ID
      {
        get
        {
          return _ID;
        }
        set
        {
          _ID = value;
        }
      }
    
      // ... other non-key fields.
    
      public DBDefinitionString(ILanguageString other)
      {
        _ID = other.ID;
        // ...
      }
    
      public DBDefinitionString()
      {
        _ID = String.Empty;
        // ...
      }
    }
    

    3. I pass a List<DBDefinitionString> to GetTable<DBDefinitionString>().InsertAllOnSubmit

    4. I call DataContext.SubmitChanges and get the exception: Violation of PRIMARY KEY constraint 'PK_DBDefinitionString'. Cannot insert duplicate key in object 'dbo.DBDefinitionString'.

    To confirm that I don't have a duplicate ID field, I inserted some code like:

    for (index = 0; index < count; index++)
    {
      ILanguageString definitionString = definitionStrings[index];
      ILanguageString ts;
    
      if (dictionary.TryGetValue(definitionString.ID, out ts))
        throw new ModelException("Duplicate key: " + definitionString.ID);
      else
        dictionary.Add(definitionString.ID, definitionString);
    }
    

    Any tips on what is wrong or how I can debug this?

    Thanks.

    -John

     

     

    Thursday, January 20, 2011 2:41 PM

Answers

  • Hi JTGame,

    Welcome to MSDN forums.

    Thanks for your feedback, I think your computer will need to have unicode chinese install to compare strings. This can be done by selecting "File" - "Windows Update", then download the Simplified Chinese Language support.

    You can try it, I am not sure it can works, I compare the two strings, there are different on my computer.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, January 21, 2011 5:49 AM
    Moderator

All replies

  • The fact that the keys have embedded Chinese characters seems to be significant.  If I replace the Chinese part with an incrementing number, the error goes away.

    Here are two keys that I think are being considered duplicate:

    zh-CHS.Word.zh-CHS.⿔.1.Meaning

    zh-CHS.Word.zh-CHS.〇.1.Meaning

    It's as if the comparison stopped at the Chinese part.

    Is this an internationalization issue?  Basically I need to be able to mix any kind of characters, because the keys could include characters from any language in the same table.

     

    Thursday, January 20, 2011 3:50 PM
  • Hi JTGame,

    Welcome to MSDN forums.

    Thanks for your feedback, I think your computer will need to have unicode chinese install to compare strings. This can be done by selecting "File" - "Windows Update", then download the Simplified Chinese Language support.

    You can try it, I am not sure it can works, I compare the two strings, there are different on my computer.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, January 21, 2011 5:49 AM
    Moderator
  • Hi JTGame,

     I am writing to check the status of the issue on your side.  Would you mind letting us 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.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, January 28, 2011 1:14 AM
    Moderator