.NET Entity Framework Extensions >.NET Entity Framework Extension What's Entity Framework Extensions? The library enhances and optimizes Entity Framework's performance by using bulk operations. var context = new CustomerContext(); //... context code... // Save entities in Bulk to increase SaveChanges performance context.bulksavechanges(); // Perform specific bulk operations context.bulkdelete(customers); context.bulkinsert(customers); context.bulkupdate(customers); context.bulkmerge(customers); // Customize operations for performance tuning context.bulksavechanges(operation => operation.batchsize = 100); // Scalable customization such as primary key context.bulkmerge(customers, operation => { operation.columnprimarykeyexpression = customer => customer.code; http://www.zzzprojects.com 1 ZZZ Projects Inc.
.NET Entity Framework Extensions > BulkSaveChanges Method BulkSaveChanges INSERT entities in a database table or a view. var context = new CustomerContext(); //... context code... context.bulksavechanges(); // Customize operation context.bulksavechanges(operation => operation.batchsize = 100); http://www.zzzprojects.com 2 ZZZ Projects Inc.
.NET Entity Framework Extensions > Bulk Operations Methods BulkInsert INSERT entities in a database table or a view. BulkUpdate UPDATE entities in a database table or a view. BulkDelete DELETE entities in a database table or a view. BulkMerge UPSERT operation. INSERT new entities and UPDATE entities matching the primary key in a database table or a view. context.bulkinsert(customers); // Customize operation. context.bulkinsert(customers, operation => { operation.batchsize = 100; context.bulkupdate(customers); // Customize operation. context.bulkupdate(customers, operation => { operation.batchsize = 100; operation.columnprimarykeyexpression = customer => customer.code; context.bulkdelete(customers); // Customize operation. context.bulkdelete(customers, operation => { operation.batchsize = 100; operation.columnprimarykeyexpression = customer => customer.code; context.bulkmerge(customers); // Customize operation. context.bulkmerge(customers, operation => { operation.batchsize = 100; operation.columnprimarykeyexpression = customer => customer.code; http://www.zzzprojects.com 3 ZZZ Projects Inc.
.NET Entity Framework Extensions > FromQuery Methods DeleteFromQuery DELETE rows in a database table or a view using Lambda Expressions without loading entities in the DbContext. UpdateFromQuery UPDATE rows in a database table or a view using Lambda Expressions without loading entities in the DbContext. var context = new CustomerContext(); // DELETE all customers that are inactive for more than 2 years context.customers.where(x => x.lastlogin < DateTime.Now.AddYears(-2)).DeleteFromQuery(); // Customize operation context.customers.where(x => x.lastlogin < DateTime.Now.AddYears(-2)).DeleteFromQuery(operation => operation.batchsize = 100); // UPDATE all customers that are inactive for more than 2 years // SET status active to false context.customers.where(x => x.actif && x.lastlogin < DateTime.Now.AddYears(-2)).UpdateFromQuery(x => new Customer {Actif = false http://www.zzzprojects.com 4 ZZZ Projects Inc.
.NET Entity Framework Extensions > Caching FromMemoryCache Cache and retrieve entities from the memory cache. // Load entities from the database once, // Then load entities from Memory cache for subsequent calls. var states = context.states.where(x => x.actif).frommemorycache(); // Customize caching settings such as key to use. var statesactif = context.states.where(x => x.actif).frommemorycache("states_actif"); FromSessionCache Cache and retrieve entities from the session cache. FromHttpRequestCache Cache and retrieve entities from the HttpRequest cache. FromHttpRuntimeCache Cache and retrieve entities from the HttpRuntime cache. var states = context.states.where(x => x.actif).fromsessioncache(); var states = context.states.where(x => x.actif).fromhttprequestcache(); var states = context.states.where(x => x.actif).fromhttpruntimecache(null, DateTime.Now.AddHours(1), TimeSpan.Zero, CacheItemPriority.Default, null); http://www.zzzprojects.com 5 ZZZ Projects Inc.
.NET Entity Framework Extensions > Execute Methods ExecuteDataSet Execute a SQL statement and return all results sets in a DataSet. var sql = "SELECT * FROM Customer WHERE Actif = @IsActif"; // Customizing command with parameters. var ds = context.database.executedataset(command => { command.commandtext = sql; var parameter = command.createparameter(); parameter.parametername = "@IsActif"; parameter.value = true; ExecuteDataTable Execute a SQL statement and return a DataTable. ExecuteEntity<TEntity> Execute a SQL statement and return a TEntity. ExecuteEntities<TEntity> Execute a SQL statement and return a List<TEntity>. ExecuteEntityWithMapping<TEntity> Execute a SQL statement and return a TEntity using Entity Framework Mapping. ExecuteEntitiesWithMapping<TEntity> Execute a SQL statement and return a List<TEntity> using Entity Framework Mapping. ExecuteExpandoObject Execute a SQL statement and return an ExpandoObject. ExecuteExpandoObjects Execute a SQL statement and return a List<ExpandoObject>. command.parameters.add(parameter); var sql = "SELECT * FROM Customer"; var dt = context.database.executedatatable(sql); var sql = "SELECT TOP 1 * FROM Customer"; Customer entity = context.database.executeentity<customer>(sql); var sql = "SELECT * FROM Customer"; List<Customer> entities = context.database.executeentities<customer>(sql); var sql = "SELECT TOP 1 * FROM Customer"; Customer entity = context.database.executeentitywithmapping<customer>(sql); var sql = "SELECT * FROM Customer"; List<Customer> entities = context.database.executeentitieswithmapping<customer>(sql); var sql = "SELECT TOP 1 * FROM Customer"; ExpandoObject expandoobject = context.database.executeexpandoobject(sql); var sql = "SELECT * FROM Customer"; List<ExpandoObject> expandoobjects = context.database.expandoobjects(sql); http://www.zzzprojects.com 6 ZZZ Projects Inc.
.NET Entity Framework Extensions > Execute Methods ExecuteNonQuery Execute a SQL statement and return the number of rows affected. ExecuteReader Execute a SQL Statement and perform an action on the DataReader. ExecuteScalar Execute a SQL statement and return an object. ExecuteScalarAs<TValue> Execute a SQL statement and return a TValue using cast. ExecuteScalarTo<TValue> Execute a SQL statement and return a TValue using convert. ExecuteXmlReader Execute a SQL statement and return a XmlReader. var sql = "DELETE FROM Customer"; int rowaffected = context.database.executenonquery(sql); var sql = "SELECT COUNT(1) FROM Customer"; context.database.executereader(sql, reader => { while (reader.read()) { //... code... } var sql = "SELECT COUNT(1) FROM Customer"; object value = context.database.executescalar(sql); var sql = "SELECT COUNT(1) FROM Customer"; int value = context.database.executescalaras<int>(sql); var sql = "SELECT COUNT(1) FROM Customer"; int value = context.database.executescalarto<int>(sql); var sql = "SELECT * FROM Customer"; XmlReader reader = context.database.executexmlreader(sql); http://www.zzzprojects.com 7 ZZZ Projects Inc.
.NET Entity Framework Extensions > Extension Methods Database Retrieve the model behind the context at runtime. var database = new CustomerContext().Database; // Get Context var context = database.getcontext<customercontext>(); var dbcontext = database.getdbcontext(); // Get Connection var connection = database.getconnection<sqlconnection>(); var entityconnection = database.getentityconnection(); DbContext Get a specific entity from the model. // Get Transaction var transaction = database.gettransaction<sqltransaction>(); var dbtransaction = database.getdbtransaction(); var entitytransaction = database.getentitytransaction(); var dbcontext = new CustomerContext(); // Get Model var model = dbcontext.getmodel(); // Get Context var objectcontext = dbcontext.getobjectcontext(); DbSet Get a specific property from an entity. // Convert ToData var ds = dbcontext.todataset(customers); var dt = dbcontext.todatatable(customers); var dbcontext = new CustomerContext(); // Add or Update entities dbcontext.customers.addorupdateextension(customer => customer.code, customers); http://www.zzzprojects.com 8 ZZZ Projects Inc.
.NET Entity Framework Extensions > Extension Methods EntityTransaction Get a specific property from an entity. IQueryable Get a specific property from an entity. ObjectContext Get a specific property from an entity. var entitytransaction = new CustomerContext().Database.GetEntityTransaction(); // Get Transaction var transaction = entitytransaction.gettransaction<sqltransaction>(); var dbtransaction = entitytransaction.getdbtransaction(); var query = new CustomerContext().Customers.Where(x => x.actif); // Get Context var context = query.getcontext(); var objectcontext = new CustomerContext().GetObjectContext(); // Get Context var dbcontext = objectcontext.getdbcontext(); http://www.zzzprojects.com 9 ZZZ Projects Inc.
.NET Entity Framework Extensions > Runtime Model GetModel Retrieve the model behind the context at runtime. Entity<TEntity> Get a specific entity from the model. Property Get a specific property from an entity. var context = new CustomerContext(); // GET model var model = context.getmodel(); var model = context.getmodel(); // GET entity and meta var customer = model.entity<customer>(); var basetype = customer.info.basetype; var istpc = customer.info.istpc; var model = context.getmodel(); var customer = model.entity<customer>(); // GET property and meta var name = customer.property(x => x.name); var ismandatory =!name.nullable; var isprimarykey = name.isprimarykey; var length = name.maxlength; http://www.zzzprojects.com 10 ZZZ Projects Inc.
.NET Entity Framework Extensions > Specialized Database SqlDatabase Get a SQL Server database instance for the context. var context = new CustomerContext(); var sql = "SELECT * FROM Customer WHERE Actif = @IsActif"; // Use specialized database to use specific class instead of abstract class var dt = context.sqldatabase().executedatatable(sql, new[] {new SqlParameter("@IsActif", true) SqlCeDatabase Get a SQL Compact database instance for the context. MySqlDatabase Get a MySQL database instance for the context. SQLiteDatabase Get a SQLite database instance for the context. // Customize command var dt2 = context.sqldatabase().executedatatable(command => { command.commandtext = sql; command.parameters.add(new SqlParameter("@IsActif", true)); var dt = context.sqlcedatabase().executedatatable(sql, new[] {new SqlCeParameter("@IsActif", true) var dt = context.mysqldatabase().executedatatable(sql, new[] {new MySqlParameter("@IsActif", true) var dt = context.sqlitedatabase().executedatatable(sql, new[] {new SQLiteParameter("@IsActif", true) http://www.zzzprojects.com 11 ZZZ Projects Inc.