CSharp · Console
// @nuget: Microsoft.EntityFrameworkCore.SqlServer
// @nuget: Z.EntityFramework.Extensions.EFCore
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Z.EntityFramework.Extensions;
public class Program
{
public static void Main()
{
using var context = new EntityContext();
context.Database.EnsureCreated();
var customers = new List<Customer>
{
new Customer { Name = "Alice", Email = "[email protected]" },
new Customer { Name = "Bob", Email = "[email protected]" }
};
// Insert all customers
context.BulkInsert(customers);
Console.WriteLine($"{customers.Count} customers inserted.");
// Query and display all customers
FiddleHelper.WriteTable(context.Customers.ToList());
}
}
public class EntityContext : DbContext
{
public DbSet<Customer> Customers { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(
FiddleHelper.GetConnectionStringSqlServer());
}
}
public class Customer
{
public int CustomerID { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}