using System.Collections.Generic;
using Z.EntityFramework.Plus;
using Microsoft.EntityFrameworkCore;
using Microsoft.Data.SqlClient;
public static void Main()
ussding (var context = new EntityContext())
context.Database.EnsureCreated();
using (var context = new EntityContext())
context.Customers.Add(new Customer() { Name ="Customer_A", IsActive = false });
using (var context = new EntityContext())
var customer = context.Customers.First();
context.Orders.Add(new Order() { Description = "This should work", Customer = customer } );
context.Orders.Add(new Order() { Description = "This should also work", Customer = customer } );
context.Orders.Add(new Order() { Description = "This will fail since there is no customer" } );
context.BulkSaveChanges();
catch (DbUpdateException ex)
var entries = ex.Entries.Select(e => ((Order)e.Entity).Description).ToArray();
FiddleHelper.Dump(entries);
public class EntityContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
optionsBuilder.UseSqlServer(new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()));
base.OnConfiguring(optionsBuilder);
public DbSet<Customer> Customers { get; set; }
public DbSet<Order> Orders {get; set;}
public int CustomerID { get; set; }
public string Name { get; set; }
public Boolean IsActive { get; set; }
public ICollection<Order> Orders {get; set;}
public int OrderID {get; set;}
public string Description {get; set;}
public int CustomerID {get; set;}
public Customer Customer {get; set;}