我正在尝试使用一些数据来播种数据库。我得到的错误是在布尔值和日期字段上。下面是播种数据库的示例种子数据。我做错了什么?
Sql("INSERT INTO USERS (Id,FirstName,LastName,Email,RefIndicator,TeamId,IsRegistered,DateRegistered,LastModified,UserRolesId) VALUES (1, 'SomeFirstName', 'SomeSurName', 'name@somedomainname.com', 'IMT/iTC22', 1, True, convert(datetime, '17/07/2017 17:56:25'), null, 1)");
Sql("INSERT INTO USERS (Id,FirstName,LastName,Email,RefIndicator,TeamId,IsRegistered,DateRegistered,LastModified,UserRolesId) VALUES (2, 'SomeFirstName', 'SomeSurName', 'name@somedomainname.com', 'IMT/iTC22', 1, True, convert(datetime, '17/07/2017 17:56:25'), null, 2)");
Sql("INSERT INTO USERS (Id,FirstName,LastName,Email,RefIndicator,TeamId,IsRegistered,DateRegistered,LastModified,UserRolesId) VALUES (3, 'SomeFirstName', 'SomeSurName', 'name@somedomainname.com', 'IMT/iTC22', 1, True, convert(datetime, '17/07/2017 17:56:25'), null, 3)");
下面是我需要填充种子数据的表格结构
CREATE TABLE [dbo].[Users] (
[Id] TINYINT NOT NULL,
[FirstName] NVARCHAR (MAX) NULL,
[LastName] NVARCHAR (MAX) NULL,
[Email] NVARCHAR (MAX) NULL,
[RefIndicator] NVARCHAR (MAX) NULL,
[TeamId] TINYINT NOT NULL,
[IsRegistered] BIT NOT NULL,
[DateRegistered] DATETIME NOT NULL,
[LastModified] DATETIME NULL,
[UserRolesId] TINYINT DEFAULT ((0)) NOT NULL,
CONSTRAINT [PK_dbo.Users] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_dbo.Users_dbo.Teams_TeamId] FOREIGN KEY ([TeamId]) REFERENCES [dbo].[Teams] ([Id]) ON DELETE CASCADE,
CONSTRAINT [FK_dbo.Users_dbo.UserRoles_UserRolesId] FOREIGN KEY ([UserRolesId]) REFERENCES [dbo].[UserRoles] ([Id]) ON DELETE CASCADE
);
GO
CREATE NONCLUSTERED INDEX [IX_TeamId]
ON [dbo].[Users]([TeamId] ASC);
GO
CREATE NONCLUSTERED INDEX [IX_UserRolesId]
ON [dbo].[Users]([UserRolesId] ASC);
答案 0 :(得分:1)
您正尝试将值True
插入IsRegistered
类bit
列。您正在执行脚本字符串,因此未发送C#布尔值true
,但正在发送未包含在单引号中的字符串True
。这就是两个值都抛出错误的原因。
将您的True
替换为字符串中的1
,您就可以了。
答案 1 :(得分:0)
我使用下面的代码成功播种了数据库
Sql("INSERT INTO USERS (Id,FirstName,LastName,Email,RefIndicator,TeamId,IsRegistered,DateRegistered,LastModified,UserRolesId) VALUES (1, 'SampleName', 'SampleSurname', 'someNames@nnxg.com', 'IMT/iTC22', 1, 'True', GETDATE(), null, 1)");
Sql("INSERT INTO USERS (Id,FirstName,LastName,Email,RefIndicator,TeamId,IsRegistered,DateRegistered,LastModified,UserRolesId) VALUES (2, 'SampleName', 'SampleSurname', 'someNames@nnxg.com', 'IMT/iTC22', 1, 'True', GETDATE(), null, 2)");
Sql("INSERT INTO USERS (Id,FirstName,LastName,Email,RefIndicator,TeamId,IsRegistered,DateRegistered,LastModified,UserRolesId) VALUES (3, 'SampleName', 'SampleSurname', 'someNames@nnxg.com', 'IMT/iTC22', 1, 'True', GETDATE(), null, 3)");
我将True的字符串值更改为布尔值' True'并且还使用GETDATE()函数将当前日期时间值插入数据库。我从这个post
中选择了日期解决方案