locked
Cannot insert explicit value for identity column in table 'PhotoAlbumTable' when IDENTITY_INSERT is set to OFF. RRS feed

  • Question

  • User1661955822 posted

    I've been trying to figure out how to fix this for 4 hours now,.

    I have  my table's primary keys set to is identity YES for automatic assignment of a unique number, yet I'm still having this problem here's a part of my code:

    <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="ID" DataSourceID="EntityDataSource1" DefaultMode="Insert" Height="50px" Width="125px">
            <Fields>
                <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" InsertVisible="false"/>
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name"  />
                <asp:CommandField ShowInsertButton="True" />
            </Fields>
        </asp:DetailsView>
        <asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=AnimeCoffeeEntities" DefaultContainerName="AnimeCoffeeEntities" EnableFlattening="False" EntitySetName="PhotoAlbumTables" OnInserted="EntityDataSource1_Inserted" EnableInsert="True">
        </asp:EntityDataSource>


    and here's my code behind:

     protected void EntityDataSource1_Inserted(object sender, EntityDataSourceChangedEventArgs e)
        {
            if (e.Entity != null)
            {
                PhotoAlbumTable myPhotoAlbum = (PhotoAlbumTable)e.Entity;
                Response.Redirect(string.Format("~/Demos/ManagePhotoAlbum.aspx?PhotoAlbumID={0}", myPhotoAlbum.ID.ToString()));
            }
        }

    Saturday, March 22, 2014 2:14 PM

Answers

  • User1661955822 posted

    Hey Guys I got it now,
     
    It happened because I made my Primary keys set to is Identity AFTER I made an Entity FrameWork Diagram to hook it in my controls, so the Entity FrameWork Diagram is not updated with who are the "is Identity" columns. After updating the diagram magically everything worked fine.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 23, 2014 8:02 PM

All replies

  • User753101303 posted

    Hi,

    Do you use fluent api to configure your entities ? I would say this column is not marked as being store generated so EF does take this column into account when it shouldn't. How do have configured this entitty ?

    Saturday, March 22, 2014 2:38 PM
  • User1661955822 posted

    Hey Guys I got it now,
     
    It happened because I made my Primary keys set to is Identity AFTER I made an Entity FrameWork Diagram to hook it in my controls, so the Entity FrameWork Diagram is not updated with who are the "is Identity" columns. After updating the diagram magically everything worked fine.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 23, 2014 8:02 PM