当IDENTITY_INSERT时,无法在表'PhotoAlbumTable'中为标识列插入显式值... [英] Cannot insert explicit value for identity column in table 'PhotoAlbumTable' when IDENTITY_INSERT...

查看:80
本文介绍了当IDENTITY_INSERT时,无法在表'PhotoAlbumTable'中为标识列插入显式值...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想弄清楚如何解决这个问题4个小时了。



我将我的表的主键设置为是自动的身份是分配一个唯一的号码,但我仍然遇到这个问题,这是我的代码的一部分:



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()));
        }
    }

推荐答案

您正在为标识列插入值。但是您已在该列上启用了标识插入。



而是打开这样的表格上的身份插入,这样你就可以指定自己的身份值。



You're inserting values for the identity column. But you have turned on identity insert on that column.

Instead turn on identity insert on the table like this so that you can specify your own identity values.

SET IDENTITY_INSERT PhotoAlbumTable ON

INSERT INTO PhotoAlbumTables
/*Note the column list is REQUIRED here, not optional*/
            (ID, Name)
VALUES      (20, 'John')

SET IDENTITY_INSERT PhotoAlbumTable OFF 





[请接受/上调 - 投票答案或解决方案,为您鼓励parti cipation]



[Please accept/up-vote answers or solutions that work for you to encourage participation]


嘿伙计们我现在知道了,



之所以发生这种情况,是因为我将我的主键设置为是我之后的身份制作了一个实体框架图以将其挂钩到我的控件中,因此实体框架图不会更新谁是是身份列。神奇地更新图表之后一切正常。
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.


这篇关于当IDENTITY_INSERT时,无法在表'PhotoAlbumTable'中为标识列插入显式值...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆