CSharp · Console
// @nuget: EntityFramework
// @nuget: Z.EntityFramework.Extensions
// @nuget: Z.EntityFramework.Plus.EF6
using System;
using System.Linq;
using System.Data.Entity;
using Z.EntityFramework.Plus;
public class Program
{
public static void Main()
{
GenerateData();
// Update: UPDATE all rows from the database using a LINQ Query without loading entities in the context.
using (var context = new EntityContext())
{
// UPDATE all customers that are inactive for more than two years
context.Customers
.Update(x => new Customer {IsActive = false, Description = "Updated", Count = x.Count+1});
FiddleHelper.WriteTable(context.Customers.ToList());
}
using (var context = new EntityContext())
{
context.Customers
.Update(x => new Customer {IsActive = false, Description = "Updated", Count = x.Count+1});
FiddleHelper.WriteTable(context.Customers.ToList());
}
}
public static void GenerateData()
{
using (var context = new EntityContext())
{
context.Customers.Add(new Customer() { Name ="Customer_A", Description = "Description",Count =0, IsActive = true, LastLogin = new DateTime(2010,1,1) });
context.Customers.Add(new Customer() { Name ="Customer_B", Description = "Description",Count =0, IsActive = true, LastLogin = new DateTime(2010,1,1) });
context.Customers.Add(new Customer() { Name ="Customer_C", Description = "Description",Count =0, IsActive = true, LastLogin = DateTime.Now });
context.BulkSaveChanges();
}
using (var context = new EntityContext())
{
FiddleHelper.WriteTable(context.Customers.ToList());
}
}
public class EntityContext : DbContext
{
public EntityContext() : base(FiddleHelper.GetConnectionStringSqlServer())
{
}
public DbSet<Customer> Customers { get; set; }
}
public class Customer
{
public int CustomerID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public DateTime LastLogin { get; set; }
public int Count {get;set;}
public Boolean IsActive { get; set; }
}
}