在Entity Framework中实现指定字段更新

public void UpdateBlogCoinfigLastUpdatedTest() { using (BlogDbContext context = new BlogDbContext()) { var blog = new Blog() { BlogID = 0 , LastModified = DateTime.Now }; context.BlogConfigs.Attach(blog); var stateEntry = ((IObjectContextAdapter)context).ObjectContext. ObjectStateManager.GetObjectStateEntry(blog); stateEntry.SetModifiedProperty( " LastUpdated " ); context.SaveChanges(); } }
复制代码

EF生成的SQL语句如下:

exec sp_executesql N ' update [dbo].[blog_Config] set [LastUpdated] = @ 0 where ([BlogID] = @ 1 ) ' ,N ' @ 0 datetime2( 7 ),@ 1 int ' ,@0= ' 2011 - 04 - 06 14 : 12 : 28.2129938 ' ,@1=0

你可能感兴趣的:(framework)