我想我可能在Entity Framework中发现了一个错误。
我正在使用Database First和MVC3。
我的数据库中有以下表格:
CREATE TABLE [dbo].[InstructionStep](
[instructionID] [uniqueidentifier] NOT NULL,
[step] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
[description] [nvarchar](max) NULL,
[recurrence] [numeric](18, 0) NULL,
[parameters] [nvarchar](50) NULL,
[secuence] [numeric](18, 0) NULL,
[time] [nvarchar](50) NULL,
CONSTRAINT [PK_InstructionStep] PRIMARY KEY CLUSTERED
(
[instructionID] ASC,
[step] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY
我正在使用linq to EF将信息保存/编辑到此表中。事情是...“时间”列,永远不会得到一个值。我进行了三重检查并且模型DOES具有对象的time属性的值,但是当我将它传递给EF时,它永远不会进入数据库。这是我用来创建新记录的代码:
using (Models.SAIMBehrEntities db = new Models.SAIMBehrEntities())
{
if (step.description != null)
step.description = step.description.ToUpper();
if (step.parameters != null)
step.parameters = step.parameters.ToLower();
db.InstructionSteps.AddObject(step);
db.SaveChanges();
}
这是我用来编辑记录的代码:
Models.InstructionStep s = db.InstructionSteps.Where(x => x.instructionID == stepM.instructionID && x.step == stepM.step).FirstOrDefault();
if (s != null)
{
s.parameters = stepM.parameters;
s.description = stepM.description;
s.recurrence = stepM.recurrence;
s.secuence = stepM.secuence;
s.time = stepM.time;
db.SaveChanges();
}
除“时间”列外,其他所有内容都会被保存。现在,我启动了sql profiler。这是create方法生成的查询:
exec sp_executesql N'insert [dbo].[InstructionStep]([instructionID], [description], [recurrence], [parameters], [secuence])
values (@0, @1, @2, @3, @4)
select [step], [time]
from [dbo].[InstructionStep]
where @@ROWCOUNT > 0 and [instructionID] = @0 and [step] = scope_identity()',N'@0 uniqueidentifier,@1 nvarchar(max) ,@2 decimal(18,0),@3 nvarchar(50),@4 decimal(18,0)',@0='9013B4B0-28FB-4C52-912D-3598A8E13880',@1=N'DEMO DESCRIPTION',@2=1,@3=N'none',@4=1
这是更新方法生成的查询:
exec sp_executesql N'update [dbo].[InstructionStep]
set [description] = @0, [recurrence] = @1, [parameters] = @2, [secuence] = @3
where (([instructionID] = @4) and ([step] = @5))
select [time]
from [dbo].[InstructionStep]
where @@ROWCOUNT > 0 and [instructionID] = @4 and [step] = @5',N'@0 nvarchar(max) ,@1 decimal(18,0),@2 nvarchar(50),@3 decimal(18,2),@4 uniqueidentifier,@5 decimal(18,0)',@0=N'DEMO DESCRIPTION',@1=1,@2=N'none',@3=1.00,@4='9013B4B0-28FB-4C52-912D-3598A8E13880',@5=1386
所以......是的在更新/插入后选择“时间”列。为什么?打败我...
我尝试将列名更改为“timestep”并正常工作。
这是正常行为吗?我只是愚蠢或者这是一个错误吗?
谢谢!