CSharp · Console
// @nuget: EntityFramework
// @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();
// Delete: DELETE all rows from the database using a LINQ Query without loading entities in the context
using (var context = new EntityContext())
{
// DELETE all customers that are inactive
context.Customers.Where(x => !x.IsActive).Delete();
FiddleHelper.WriteTable(context.Customers.ToList());
}
// Delete: DELETE all rows from the database using a LINQ Query without loading entities in the context
using (var context = new EntityContext())
{
// DELETE customers by Name
context.Customers.Where(x => x.Name =="Customer_B").Delete();
FiddleHelper.WriteTable(context.Customers.ToList());
}
}
public static void GenerateData()
{
using (var context = new EntityContext())
{
context.Customers.Add(new Customer() { Name ="Customer_A", IsActive = false });
context.Customers.Add(new Customer() { Name ="Customer_B", IsActive = true });
context.Customers.Add(new Customer() { Name ="Customer_C", IsActive = false });
context.SaveChanges();
}
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 Boolean IsActive { get; set; }
}
}