简体   繁体   English

在SQL Server中使用身份

[英]Using identity in sql server

I have a table of 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)
);

And I want the key field "OwnerID" to auto increment (starting from 1), but it doesn't work. 而且我希望键字段“ OwnerID”自动递增(从1开始),但是它不起作用。

The c# code is: 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;

When I debug I can see that the field OwnerID is set to 0 (I don't know why) and at the end I have an error message: 调试时,我可以看到OwnerID字段设置为0(我不知道为什么),最后我收到一条错误消息:

The server was unable to process the request due to an internal error. 由于内部错误,服务器无法处理请求。 For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework SDK documentation and inspect the server trace logs. 有关错误的更多信息,请打开服务器上的IncludeExceptionDetailInFaults(从ServiceBehaviorAttribute或从配置行为),以便将异常信息发送回客户端,或者按照Microsoft .NET Framework SDK文档和检查服务器跟踪日志。

If your using EF then you should create a Configuration for Owner : 如果使用EF,则应为Owner创建一个配置:

class OwnerConfiguration: EntityTypeConfiguration<Owner>
    {
        public OwnerConfiguration()
        {
            HasKey(x => x.OwnerId);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM