数据库中字段设置的默认值和插入的结果不一致
表Persons中有一字段isStudent,bit类型,not null,默认值设置为1.但是在程序中执行添加Persons操作时,不给isStudent赋值,默认值都是0.
求解… --------------------编程问答-------------------- insert的sql语句贴出来 --------------------编程问答-------------------- 先看看你的添加代码是不是有问题,怎么写的 --------------------编程问答-------------------- 我做插入不是直接用sql插的,而是运用EntityFramework
代码如下:
using (TestEntities context = new TestEntities())
{
var person = context.CreateObject<PersonsEFEntity>();
person.FirstName = "w";
person.LastName = "m";
person.Address = "闵行宜山路";
person.City = "上海";
context.PersonsEFEntitySet.AddObject(person);
context.SaveChanges();
}
以上代码中我没有给person.isStudent赋值 --------------------编程问答--------------------
你确定isStudent默认值是1不是0 --------------------编程问答-------------------- person.isStudent不赋值就是默认为0吧 --------------------编程问答-------------------- 数据库脚本:SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Persons](
[Id] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [nvarchar](50) NULL,
[LastName] [nvarchar](50) NULL,
[Address] [nvarchar](50) NULL,
[City] [nvarchar](50) NULL,
[isStudent] [bit] NOT NULL,
CONSTRAINT [PK_Persons] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Persons] ADD CONSTRAINT [Persons_isStudent] DEFAULT ((1)) FOR [isStudent]
GO
默认值是1, --------------------编程问答-------------------- EntityFramework!~没用过。你看看person的实体里面是不是给isStudent设置了默认值。
补充:.NET技术 , ASP.NET