【问题标题】:Adding EntityFramework.Extended to the project将 EntityFramework.Extended 添加到项目中
【发布时间】:2012-12-19 22:37:40
【问题描述】:

我已使用 NuGet 将EntityFramework.Extended (link) 添加到我的项目中。 现在我面临一个问题;如何将它的 Update 函数与我的 dbContext 一起使用?

【问题讨论】:

  • 他们在同一页上有一些例子:github.com/loresoft/EntityFramework.Extended
  • 我已经看到了,但这并不能帮助我理解如何将方法添加/扩展到 dbContext 中。是我遗漏了什么还是我缺乏一些知识?

标签: c# entity-framework


【解决方案1】:

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

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

【讨论】:

  • 哇我的.. 我已经尝试了一百万次,但更新方法从未出现过。 #facepalm 谢谢...
【解决方案2】:

您像这样为 Update 指定 Where 谓词:

context.tblToUpdate
       .Update(entry => condition, entryWithnewValues => new tblToUpdate{});

【讨论】:

    【解决方案3】:

    更新:

    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 不需要什么特别的

    【讨论】:

      【解决方案4】:

      DbContext 上的 Extensions 方法现已消失。

      支持的扩展方法仅在 IQueryable 上

      【讨论】:

      • 此答案与旧版 EntityFramework.Extended 库无关。它不能解决问题中描述的问题。这可能是对问题的评论。为什么你认为遗留扩展支持 DbContext? dbContext.Users.Update 不是 DbContext 扩展
      猜你喜欢
      • 2021-09-17
      • 2018-09-04
      • 2016-11-02
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多