Reputation: 629
I've added EntityFramework.Extended
(link) to my project using NuGet.
Now I'm facing one single problem; how can I use it's Update
function with my dbContext
?
Upvotes: 3
Views: 4419
Reputation: 2496
The Extensions methods on DbContext are now gone away.
The Extensions methods supported are only on IQueryable
Upvotes: -1
Reputation: 3818
Update with:
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"});
You should have a class that inherits DbContext and has public DbSets like:
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; }
}
}
Nothing special is required for using EntityFramework.Extended
Upvotes: 0
Reputation: 1517
You specify Where predicate for Update like so:
context.tblToUpdate
.Update(entry => condition, entryWithnewValues => new tblToUpdate{});
Upvotes: 2
Reputation: 236318
Import namespace using EntityFramework.Extensions
and use Update
(sample from Update method description):
dbContext.Users.Update(
u => u.Email.EndsWith(emailDomain),
u => new User { IsApproved = false, LastActivityDate = DateTime.Now });
Upvotes: 8