将EntityFramework.Extended添加到项目中
我已经使用NuGet向我的项目添加了EntityFramework.Extended
( link )。 现在我面临一个问题; 如何在我的dbContext
使用它的Update
function?
using EntityFramework.Extensions
导入名称空间并使用Update
(来自Update方法描述的示例):
dbContext.Users.Update( u => u.Email.EndsWith(emailDomain), u => new User { IsApproved = false, LastActivityDate = DateTime.Now });
您可以为Update指定Where谓词,如下所示:
context.tblToUpdate .Update(entry => condition, entryWithnewValues => new tblToUpdate{});
更新:
YourDbContext context=new YourDbContext(); //update all tasks with status of 1 to status of 2 context.YourModels.Update( t => t.StatusId == 1, t2 => new Task {StatusId = 2}); //example of using an IQueryable as the filter for the update var yourmodel = context.YourModels.Where(u => u.FirstName == "firstname"); context.YourModels.Update(yourmodel , u => new YourModel {FirstName = "newfirstname"});
您应该有一个inheritanceDbContext并具有公共DbSets的类,如:
using System; using System.Collections.Generic; using System.Linq; using System.Data.Entity; namespace YourNamespace.Models { public class YourDBContext: DbContext { public DbSet YourModels { get; set; } } }
使用EntityFramework.Extended没有什么特别之处
DbContext上的Extensions方法现在消失了。
支持的扩展方法仅适用于IQueryable