向项目中添加EntityFramework.Extended

3
我已经通过NuGet添加了 EntityFramework.Extended (link) 到我的项目中。 现在,我只面临一个问题:如何在我的 dbContext 中使用它的 Update 函数?

他们在同一页上提供了一些示例:https://github.com/loresoft/EntityFramework.Extended - Andrei Drynov
我已经看过了,但这并没有帮助我理解如何像他们一样添加/扩展dbContext的方法。 我是不是漏掉了什么或者缺乏某些知识? - NucS
4个回答

8

导入命名空间using EntityFramework.Extensions并使用Update(来自Update方法描述的示例):

dbContext.Users.Update(
         u => u.Email.EndsWith(emailDomain),
         u => new User { IsApproved = false, LastActivityDate = DateTime.Now });

哦,我的天啊...我已经尝试了无数次,但更新方法从未出现。 #facepalm 谢谢... - NucS

2
您可以这样指定Update的Where谓词:
context.tblToUpdate
       .Update(entry => condition, entryWithnewValues => new tblToUpdate{});

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并具有公共DbSet的类,例如:

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没有特殊要求。


-1

DbContext 上的扩展方法现在已经消失了。

支持的扩展方法只有在 IQueryable 上。

enter image description here


这个答案与遗留的EntityFramework.Extended库无关。它不能解决问题描述中的问题。它可能是对问题的评论。而且你为什么认为遗留扩展支持DbContext?dbContext.Users.Update不是一个DbContext扩展。 - Sergey Berezovskiy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接