Note that there are some explanatory texts on larger screens.

plurals
  1. POWeird behaviour in EF (possible bug)
    text
    copied!<p>I think I might have found a bug in Entity Framework.</p> <p>I'm using Database First with MVC3.</p> <p>I have the following table in my database:</p> <pre><code>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 </code></pre> <p>I'm using linq to EF to save/edit information into this table. The thing is... the "time" column, NEVER gets a value. I triple checked and the model DOES have a value for the time property of the object, but when I pass it to EF, it never makes it to the database. This is the code I use to create a new record:</p> <pre><code> 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(); } </code></pre> <p>this is the code I use to edit a record:</p> <pre><code> Models.InstructionStep s = db.InstructionSteps.Where(x =&gt; x.instructionID == stepM.instructionID &amp;&amp; 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(); } </code></pre> <p>Everything else gets saved, except the "time" column. Now, I fired up sql profiler. This is the query generated by the create method:</p> <pre><code>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 &gt; 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 </code></pre> <p>And this is the query generated by the update method:</p> <pre><code>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 &gt; 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 </code></pre> <p>So... yeah. The "time" column gets selected after an update/insert. Why? beats me...</p> <p>I tried changing the column name to "timestep" and worked correctly.</p> <p>Is this a normal behaviour? am I just being plain stupid or is this a bug?</p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload