Targeted Operations
This feature allows targeted invocation when executing operations against the database. It is particularly useful when performance and efficiency matter, as targeting specific columns eliminates unnecessary network traffic.
This is especially relevant when your table or entity model has many columns and you only need to query, insert, merge, or update a subset of them.
Why Targeted Operations?
A “macro” ORM (e.g., Entity Framework, NHibernate) gets the same “touch only what changed” behavior from a change tracker attached to its DbContext/session — it snapshots every attached entity, diffs it at SaveChanges() time, and generates SQL for only the columns that moved. That convenience is bought with real machinery: an identity map, proxy or snapshot-based interception, and a stateful unit of work you must manage across the request’s lifetime.
RepoDB is a micro ORM — there is no attached context and nothing snapshotting your entities in the background. Targeted Operations is the fluent, explicit substitute: rather than the library inferring which columns changed by diffing against a tracked copy, you simply declare which columns to touch, per call, via the fields parameter. It is the same “only send what I care about” outcome, just decided by the caller up front instead of inferred by the framework after the fact — which means it works identically whether the object at hand is a tracked entity, a plain POCO, a partial DTO, or an anonymous/dynamic object, none of which need a lifecycle of their own to get the benefit.
The examples below assume the [dbo].[Customer] table and Customer entity model have many columns. Each example demonstrates how to target specific columns in the operation.
Querying a Data
Use the Query operation and pass the target fields.
using (var connection = new SqlConnection(connectionString))
{
var fields = Field.Parse<Customer>(e => new
{
e.Id,
e.FirstName,
e.LastName
});
var customer = connection.Query<Customer>(10045,
fields: fields).FirstOrDefault();
}
Or via dynamics:
using (var connection = new SqlConnection(connectionString))
{
var fields = Field.From("Id", "FirstName", "LastName");
var customer = connection.Query("[dbo].[Customer]",
10045,
fields: fields).FirstOrDefault();
}
Querying Multiple Data
Use the QueryAll operation and pass the target fields.
using (var connection = new SqlConnection(connectionString))
{
var fields = Field.Parse<Customer>(e => new
{
e.Id,
e.FirstName,
e.LastName
});
var customers = connection.QueryAll<Customer>(fields: fields);
}
Or via dynamics:
using (var connection = new SqlConnection(connectionString))
{
var fields = Field.From("Id", "FirstName", "LastName");
var customers = connection.QueryAll("[dbo].[Customer]",
fields: fields);
}
Inserting a Data
Use the Insert operation and pass the target fields.
using (var connection = new SqlConnection(connectionString))
{
var entity = new Customer
{
FirstName = "John",
LastName = "Doe"
};
var fields = Field.Parse<Customer>(e => new
{
e.FirstName,
e.LastName
});
var id = connection.Insert<Customer, int>(entity,
fields: fields);
}
Or via dynamics:
using (var connection = new SqlConnection(connectionString))
{
var entity = new
{
FirstName = "John",
LastName = "Doe"
};
var id = connection.Insert<int>("[dbo].[Customer]",
entity);
}
Or via dynamics with fields:
using (var connection = new SqlConnection(connectionString))
{
var entity = new Customer
{
FirstName = "John",
LastName = "Doe"
};
var fields = Field.From("FirstName", "LastName");
var id = connection.Insert<int>("[dbo].[Customer]",
entity, fields: fields);
}
Inserting Multiple Data
Use the InsertAll operation and pass the target fields.
using (var connection = new SqlConnection(connectionString))
{
var entities = GenerateCustomers();
var fields = Field.Parse<Customer>(e => new
{
e.FirstName,
e.LastName
});
var insertedRows = connection.InsertAll<Customer, int>(entities,
fields: fields);
}
Or via dynamics:
using (var connection = new SqlConnection(connectionString))
{
var entities = GenerateCustomers();
var insertedRows = connection.InsertAll("[dbo].[Customer]",
entities);
}
Or via dynamics with fields:
using (var connection = new SqlConnection(connectionString))
{
var entities = GenerateCustomers();
var fields = Field.From("FirstName", "LastName");
var insertedRows = connection.InsertAll("[dbo].[Customer]",
entities,
fields: fields);
}
Merging a Data
Use the Merge operation and pass the target fields.
using (var connection = new SqlConnection(connectionString))
{
var entity = new Customer
{
Id = 10045,
FirstName = "John",
LastName = "Doe",
LastUpdatedDateUtc = DateTime.UtcNow
};
var fields = Field.Parse<Customer>(e => new
{
e.Id,
e.FirstName,
e.LastName,
e.LastUpdatedUtc
});
var id = connection.Merge<Customer, int>(entity,
fields: fields);
}
Or via dynamics:
using (var connection = new SqlConnection(connectionString))
{
var entity = new Customer
{
Id = 10045,
FirstName = "John",
LastName = "Doe",
LastUpdatedDateUtc = DateTime.UtcNow
};
var id = connection.Merge<int>("[dbo].[Customer]",
entity);
}
Or via dynamics with fields:
using (var connection = new SqlConnection(connectionString))
{
var entity = new Customer
{
Id = 10045,
FirstName = "John",
LastName = "Doe",
LastUpdatedDateUtc = DateTime.UtcNow
};
var fields = Field.From("Id", "FirstName", "LastName", "LastUpdatedDateUtc");
var id = connection.Merge<int>("[dbo].[Customer]",
entity,
fields: fields);
}
Merging Multiple Data
Use the MergeAll operation and pass the target fields.
using (var connection = new SqlConnection(connectionString))
{
var entities = GetCustomersWithUpdatedAddress();
var fields = Field.Parse<Customer>(e => new
{
e.Id,
e.Address
});
var mergedRows = connection.MergeAll<Customer>(entities,
fields: fields);
}
Or via dynamics:
using (var connection = new SqlConnection(connectionString))
{
var entities = GetCustomersWithUpdatedAddress();
var mergedRows = connection.MergeAll("[dbo].[Customer]",
entities);
}
Or via dynamics with fields:
using (var connection = new SqlConnection(connectionString))
{
var entities = GetCustomersWithUpdatedAddress();
var fields = Field.From("Id", "Address");
var mergedRows = connection.MergeAll("[dbo].[Customer]",
entities,
fields: fields);
}
Updating a Data
Use the Update operation and pass the target fields.
using (var connection = new SqlConnection(connectionString))
{
var entity = new Customer
{
Id = 10045,
FirstName = "John",
LastName = "Doe",
LastUpdatedDateUtc = DateTime.UtcNow
};
var fields = Field.Parse<Customer>(e => new
{
e.Id,
e.FirstName,
e.LastName,
e.LastUpdatedUtc
});
var updatedRows = connection.Update<Customer>(entity,
fields: fields);
}
Or via dynamics:
using (var connection = new SqlConnection(connectionString))
{
var entity = new Customer
{
Id = 10045,
FirstName = "John",
LastName = "Doe",
LastUpdatedDateUtc = DateTime.UtcNow
};
var updatedRows = connection.Update("[dbo].[Customer]",
entity);
}
Or via dynamics with fields:
using (var connection = new SqlConnection(connectionString))
{
var entity = new Customer
{
Id = 10045,
FirstName = "John",
LastName = "Doe",
LastUpdatedDateUtc = DateTime.UtcNow
};
var fields = Field.From("Id", "FirstName", "LastName", "LastUpdatedDateUtc");
var updatedRows = connection.Update("[dbo].[Customer]",
entity,
fields: fields);
}
Updating Multiple Data
Use the UpdateAll operation and pass the target fields.
using (var connection = new SqlConnection(connectionString))
{
var entities = GetCustomersWithUpdatedAddress();
var fields = Field.Parse<Customer>(e => new
{
e.Id,
e.Address
});
var updatedRows = connection.UpdateAll<Customer>(entities,
fields: fields);
}
Or via dynamics:
using (var connection = new SqlConnection(connectionString))
{
var entities = GetCustomersWithUpdatedAddress();
var updatedRows = connection.UpdateAll("[dbo].[Customer]",
entities);
}
Or via dynamics with fields:
using (var connection = new SqlConnection(connectionString))
{
var entities = GetCustomersWithUpdatedAddress();
var fields = Field.From("Id", "Address");
var updatedRows = connection.UpdateAll("[dbo].[Customer]",
entities,
fields: fields);
}