v3.13.5
·
856 commits
to master
since this release
Download the library here
PATCH
- ADDED: `IsReadOnly option (Only retrieve output value without performing save operation)
This option can be useful to retrieve identity value when using a custom key.
Example:
using System.Collections.Generic;
using System.Data.Entity;
using System.Windows.Forms;
using Z.BulkOperations;
namespace Z.EntityFramework.Extensions.Lab
{
public partial class Form_Request_Scenario_UserRole : Form
{
public Form_Request_Scenario_UserRole()
{
InitializeComponent();
// CLEAR
using (var ctx = new CurrentContext())
{
ctx.Users.RemoveRange(ctx.Users);
ctx.Roles.RemoveRange(ctx.Roles);
ctx.BulkSaveChanges();
}
// SEED
using (var ctx = new CurrentContext())
{
var user1 = ctx.Users.Add(new User {Name = "User_1"});
var user2 = ctx.Users.Add(new User {Name = "User_2"});
var role1 = ctx.Roles.Add(new Role {Name = "Role_1"});
var role2 = ctx.Roles.Add(new Role {Name = "Role_2"});
// Only the user1 have a role
user1.Roles = new List<Role>();
user1.Roles.Add(role1);
ctx.SaveChanges();
}
// If your project has multiple contexts, you can use the "context" variable to know which
// context type to create or get options from it.
EntityFrameworkManager.ContextFactory = context => new CurrentContext();
// TEST
using (var ctx = new CurrentContext())
{
// Importation from a list
// Create instance of existing user and create a new user
var user1 = new User {Name = "User_1"};
var user2 = new User {Name = "User_2"};
var user3 = new User {Name = "User_3"};
var users = new List<User> {user1, user2, user3};
// Create instance of existing role
var role1 = new Role {Name = "Role_1"};
var role2 = new Role {Name = "Role_2"};
user2.Roles = new List<Role> { role2 };
user3.Roles = new List<Role> { role1, role2 };
ctx.BulkMerge(users, options =>
{
options.IncludeGraph = true;
options.IncludeGraphOperationBuilder = operation =>
{
if (operation is BulkOperation<User>)
{
var bulk = (BulkOperation<User>)operation;
bulk.ColumnPrimaryKeyExpression = x => x.Name;
}
else if (operation is BulkOperation<Role>)
{
var bulk = (BulkOperation<Role>)operation;
bulk.IsReadOnly = true;
bulk.ColumnPrimaryKeyExpression = x => x.Name;
}
};
});
}
}
public class CurrentContext : DbContext
{
public CurrentContext()
: base("CodeFirstEntities")
{
}
public DbSet<User> Users { get; set; }
public DbSet<Role> Roles { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Types().Configure(x => x.ToTable(GetType().DeclaringType != null ? GetType().DeclaringType.FullName.Replace(".", "_") + "_" + x.ClrType.Name : ""));
base.OnModelCreating(modelBuilder);
}
}
public class User
{
public int ID { get; set; }
public string Name { get; set; }
public List<Role> Roles { get; set; }
}
public class Role
{
public int ID { get; set; }
public string Name { get; set; }
public List<User> Users { get; set; }
}
}
}PRO Version unlocked for the current month (October)