我已使用NuGet将EntityFramework.Extended
(link)添加到我的项目中。
现在我面临一个问题;如何在我的Update
使用dbContext
函数?
答案 0 :(得分:8)
导入名称空间using EntityFramework.Extensions
并使用Update
(来自更新方法说明的示例):
dbContext.Users.Update(
u => u.Email.EndsWith(emailDomain),
u => new User { IsApproved = false, LastActivityDate = DateTime.Now });
答案 1 :(得分:2)
为Update指定Where谓词,如下所示:
context.tblToUpdate
.Update(entry => condition, entryWithnewValues => new tblToUpdate{});
答案 2 :(得分:0)
更新:
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"});
您应该有一个继承DbContext的类,并且具有公共DbSets,如:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Entity;
namespace YourNamespace.Models
{
public class YourDBContext: DbContext
{
public DbSet<YourModel> YourModels { get; set; }
}
}
使用EntityFramework.Extended
不需要任何特殊内容答案 3 :(得分:-1)