在sql server中使用身份
本文关键字:身份 sql server | 更新日期: 2023-09-27 17:49:57
我有一个Owner表:
CREATE TABLE [dbo].[Owner] (
[OwnerID] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (50) NOT NULL,
[Username] NVARCHAR (50) NOT NULL,
[Password] NVARCHAR (50) NOT NULL,
[Phone] NVARCHAR (50) NOT NULL,
[Mail] NVARCHAR (50) NOT NULL,
[Description] NVARCHAR (50) NOT NULL,
[Image] IMAGE NULL,
PRIMARY KEY CLUSTERED ([OwnerID] ASC)
);
我想要的关键字段"OwnerID"自动增量(从1开始),但它不工作。
c#代码是:Owner owner = new Owner();
owner.Username = ((TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("UserName")).Text;
owner.Name = ((TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("Name")).Text;
owner.Password = ((TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("Password")).Text;
owner.Phone = ((TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("Phone")).Text;
owner.Mail = ((TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("Email")).Text;
owner.Description = ((TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("Description")).Text;
当我调试时,我可以看到字段OwnerID被设置为0(我不知道为什么),最后我有一个错误消息:
由于内部错误,服务器无法处理请求。要了解有关错误的更多信息,请在服务器上打开IncludeExceptionDetailInFaults(从ServiceBehaviorAttribute或从配置行为)以便将异常信息发送回客户端,或者根据Microsoft . net Framework SDK文档打开跟踪并检查服务器跟踪日志。
如果您使用EF,那么您应该为Owner
创建一个配置:
class OwnerConfiguration: EntityTypeConfiguration<Owner>
{
public OwnerConfiguration()
{
HasKey(x => x.OwnerId);
}
}