The way around this is to create a second model that has only the fields you want to update. In fact, that's probably best practice as you only query the data you really need.
>Hi everybody,
>
>In my table I have 4 columns such as EnteredBy, EnteredOn, ModifiedBy, ModifiedOn. These columns are not for manual editing, I want to set them from the code as needed.
>
>So, if I edit an existing row, I am supposed to set ModifiedOn and ModifiedBy columns and keep the existing values for EnteredBy and EnteredOn columns.
>
>My current problem is that for some reason MVC wants to change these columns also - I do not know how to correctly deal with these columns.
>
>And also, is it somehow possible for EF to generate UPDATE statement only for columns that I touched?
>
>Say, this is how my current generated update statement looks like:
>
>exec sp_executesql N'update [dbo].[Clients]
>set [client_no] = @0, [client_name] = @1, [Address] = @2, [Contact1] = null, [C1_Email] = null,
> [C1_Phone] = null, [C1_Ext] = null, [Contact2] = null, [C2_Email] = null,
>[C2_Phone] = null, [C2_Ext] = null, [EnteredBy] = null, [ModifiedBy] = null, [EnteredOn] = @3, [ModifiedOn] = @4
>where ([ClientId] = @5)
>',N'@0 smallint,@1 varchar(30),@2 varchar(max) ,@3 datetime2(7),@4 datetime2(7),
>@5 int',@0=4124,@1='Belleayre',@2='testing',@3='0001-01-01 00:00:00',@4='2013-04-03 14:05:08.7027028',@5=31
>
>while in fact the only field I attempted to change was Address column. Is there a way in EF and MVC to generate update for changed columns only?
Craig Berntson
MCSD, Microsoft .Net MVP, Grape City Community Influencer