无法通过 SQL Server Express Management Studio 设置自动增量?
我只是尝试将值插入数据库并且可以工作。现在我再次插入,并收到相同主键的错误。
我找不到任何选项可以将其更改为自动增量。
我正在通过 Linq-To-Sql 更新表。
User u = new User(email.Text, HttpContext.Current.Request.UserHostAddress,
CalculateMD5Hash(password.Text));
db.Users.InsertOnSubmit(g);
db.SubmitChanges();
我没有填写user_id
,第一次运行得很好。它变成了零。 尝试添加第二个用户时,它想再次使 ID 为 0。
我可以查询数据库并询问最高的 ID,但如果您了解自动增量,那就太远了。
我怎样才能打开这个功能?我能找到的只是用于创建表的脚本。我想保留现有的表格并简单地对其进行编辑。
I just tried inserting value in to a database and that work. Now I insert again and I get an error for identical primary key.
I can't find any option to alter it to be auto-increment.
I'm updating the table via Linq-To-Sql.
User u = new User(email.Text, HttpContext.Current.Request.UserHostAddress,
CalculateMD5Hash(password.Text));
db.Users.InsertOnSubmit(g);
db.SubmitChanges();
I didn't fill in the user_id
and it worked fine the first time. It became zero.
Trying to add a second user, it wants to make the ID 0 again.
I could query the database and ask for the highest ID, but that's going to far if you know about auto-increment.
How can I turn this on? All I can find are scripts for table creation. I'd like to keep my existing table and simply edit it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的 Linq-to-SQL 模型是如何定义的?检查
user_id
列的属性 - 它们设置为什么?在您的 Linq-to-SQL 模型中,请务必设置
自动生成的值
,将Auto-Sync
设置为OnInsert
,并且服务器数据类型也应与您的设置 (INT IDENTITY
) 匹配在 SQL Server 管理中 Studio,您需要将
user_id
列定义为INT IDENTITY
类型 - 在可视表设计器中,您需要在此处设置此属性:How is your Linq-to-SQL model defined?? Check the properties of the
user_id
column - what are they set to??In your Linq-to-SQL model, be sure to have
Auto Generated Value
set to true,Auto-Sync
set toOnInsert
, and the server data type should also match your settings (INT IDENTITY
),In SQL Server Management Studio, you need to define the
user_id
column to be of typeINT IDENTITY
- in the visual table designer, you need to set this property here:它为零,因为主键列类型有一个整数。要使用自动增量,请将表标识列设置为 ID(在表属性中选择)
It is zero because you have a integer for a primary key column type. To use auto-increment, set tables identity column to the ID (selected in the table properties)
如果你有一个适用的版本,使用 VS 编辑数据库可能会更容易,否则如果你必须在 Management Studio 中编辑它,请参阅这篇文章:
http://blogs.msdn.com/b/sqlexpress/archive/2006/11/22/connecting-to-sql-express-user-instances-in-management-studio.aspx
Would probably be easier to edit the database using VS if you have a version that will work for, otherwise if you have to edit it in management studio see this article:
http://blogs.msdn.com/b/sqlexpress/archive/2006/11/22/connecting-to-sql-express-user-instances-in-management-studio.aspx
或者,如果您无法更改属性/表字段描述,您可以手动增加 user_id 并将其传递给插入函数
Or you can increment the user_id manually and pass it to the insert function if you cannot alter the property/table field description