Entity Framework中实现指定字段更新
直接上代码:
static void Main(string[] args) { Database.SetInitializer<BaseDB>(null); BaseDB context = new BaseDB(); var book = new Book { ISBN = "isbn002", Title = "title004" }; context.BookEntity.Attach(book); var stateEntry = ((IObjectContextAdapter)context).ObjectContext. ObjectStateManager.GetObjectStateEntry(book); stateEntry.SetModifiedProperty("Title"); context.SaveChanges(); Console.ReadKey(); }
生成SQL
exec sp_executesql N'update [dbo].[Book] set [Title] = @0 where ([ISBN] = @1) ',N'@0 nvarchar(max) ,@1 nvarchar(128)',@0=N'title004',@1=N'isbn002'