none
Error : An item with the same key has already been added RRS feed

  • Question

  • I am getting the error from the most basic of operations to add an entity, Im using a VS210 / .NET 4 / ASP.NET C# Web App

            LEAP_Entities temp = new LEAP_Entities();

            tb_TEST test = new tb_TEST();
            test.TESTTEXT = "TEST";
            temp.tb_TEST.AddObject(test);
            temp.SaveChanges(); // error occurs here

    the EDML XAML for the entity is

            <EntityType Name="tb_TEST">
              <Key>
                <PropertyRef Name="ID" />
              </Key>
              <Property Name="ID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
              <Property Name="TESTTEXT" Type="nchar" MaxLength="10" />
            </EntityType>

    there are no records at all in the underlying SQL table

    ANyone got any ideas?

    Tuesday, June 28, 2011 8:59 AM

Answers

  • On 6/28/2011 4:59 AM, webinspired1 wrote:
    > I am getting the error from the most basic of operations to add an
    > entity, Im using a VS210 / .NET 4 / ASP.NET C# Web App
    >
    > LEAP_Entities temp = new LEAP_Entities();
    >
    > tb_TEST test = new tb_TEST();
    > test.TESTTEXT = "TEST";
    > temp.tb_TEST.AddObject(test);
    > temp.SaveChanges(); // error occurs here
    >
    > the EDML XAML for the entity is
    >
    > <EntityType Name="tb_TEST">
    > <Key>
    > <PropertyRef Name="ID" />
    > </Key>
    > <Property Name="ID" Type="int" Nullable="false"
    > StoreGeneratedPattern="Identity" />
    > <Property Name="TESTTEXT" Type="nchar" MaxLength="10" />
    > </EntityType>
    >
    > there are no records at all in the underlying SQL table
    >
    > ANyone got any ideas?
    >
     
    Well, if the Identity key is not incremental on the database table,
    which you would have set Identity-key (yes) (do + 1) on the properties
    in the table definition when a record is added, then all you are
    possibly doing is adding Identity-Key = 1 over, over and over and SQL
    server is not doing a +1, as an example.
     
    It has to have been  at one point one record in that table that has the
    same key as the one you are trying to add.
     
     
    Tuesday, June 28, 2011 9:55 PM

All replies

  • Hi,

    The only reason I can see that the error should come on SaveChanges is that the temp context contains an object with the key that your database generates on savechanges and returns back to the context for the test entity. Why this happends is not easy to tell.

    But first of all do you have autoincrement on the ID column in the database? If not you should add a valid key value for your ID column in your code.

    If you have autoincrement, you can check if the temp context contains an entity with the next autoincrement number, by first checking the max on your ID column in the database by using

    SELECT MAX(ID) FROM tb_TEST
    

    With this call you get the last ID for your table, you can then in your code check this when debugging by adding a watch that looks something like this and check it before SaveChanges:

    context.ObjectStateManager.GetObjectStateEntry(new EntityKey("<Entity Container Name>.tb_Test", "Id", <the result from the select + 1)))
    


    If you don't get an InvalidOperationException you have the entity in your context before you are calling save for some reason.

     

     

     

     


    --Rune
    Tuesday, June 28, 2011 9:43 PM
  • On 6/28/2011 4:59 AM, webinspired1 wrote:
    > I am getting the error from the most basic of operations to add an
    > entity, Im using a VS210 / .NET 4 / ASP.NET C# Web App
    >
    > LEAP_Entities temp = new LEAP_Entities();
    >
    > tb_TEST test = new tb_TEST();
    > test.TESTTEXT = "TEST";
    > temp.tb_TEST.AddObject(test);
    > temp.SaveChanges(); // error occurs here
    >
    > the EDML XAML for the entity is
    >
    > <EntityType Name="tb_TEST">
    > <Key>
    > <PropertyRef Name="ID" />
    > </Key>
    > <Property Name="ID" Type="int" Nullable="false"
    > StoreGeneratedPattern="Identity" />
    > <Property Name="TESTTEXT" Type="nchar" MaxLength="10" />
    > </EntityType>
    >
    > there are no records at all in the underlying SQL table
    >
    > ANyone got any ideas?
    >
     
    Well, if the Identity key is not incremental on the database table,
    which you would have set Identity-key (yes) (do + 1) on the properties
    in the table definition when a record is added, then all you are
    possibly doing is adding Identity-Key = 1 over, over and over and SQL
    server is not doing a +1, as an example.
     
    It has to have been  at one point one record in that table that has the
    same key as the one you are trying to add.
     
     
    Tuesday, June 28, 2011 9:55 PM
  • Rune

    I added a single record using SQL Manager so then

    SELECT MAX(ID) FROM tb_TEST
    
     
    returns 2, the ID field is auto increment / identity field
    I added the line you suggested
    LEAP_Entities temp = new LEAP_Entities();
    tb_TEST test = new tb_TEST();
    test.TESTTEXT = "TEST";
    temp.tb_TEST.AddObject(test);
    temp.ObjectStateManager.GetObjectStateEntry(new EntityKey("LEAPDB_ModelStoreContainer.tb_Test", "Id", 2));
    temp.SaveChanges();
    I still only get an error on the last line "an item with the same key has already been added"

    Any ideas
    Wednesday, June 29, 2011 1:51 PM