Delete
Delete one entity, a list, or every row that matches a query.
Delete Method#
To delete entities from the data store you will need to call the Delete method on the entity's repository.
The Sharp Factory App generates one Repository per entity.
You will need an instance of the RepositoryContainer class to access your repositories.
The naming convention is RepositoryContainer.{Model Name}.{Sql Schema Name}.{Entity Name}.Delete
The Delete method has three overloads:
-
The first one takes an entity to be deleted.
This overload returns a boolean. True if succeeded, false if the entity does not exist.
-
The second overload takes a list of entities and returns void.
-
The third overload takes an instance of the Query class.
This overload returns a int that represents the number of deleted records.
This is one of the most powerful features available.
Delete Single#
The following sample of C# code shows how to delete a Customer entity from the database:
// declare an instance of the Repository Container// remember this can be a singleton if you// declare it early on and reuse it for the// lifetime of your applicationvar repo = new RepositoryContainer();// declare an instance of the Customer entity// and populate it with the values// this is for demonstration purposes only// in real life this entity would come from the database// or perhaps your system had already loaded it beforehandvar customer = new Customer{ Id = 1, Name = "John", LastName = "Doe"};// call the delete method and pass the existing customervar success = repo.AppDb.Sales.Customer.Delete(customer);if (success){ // it was deleted now do something about it}Delete List#
To delete a list you can just pass the List<Customer> to the Delete method.
The following sample of C# code shows how to delete a list of customer in the database:
// declare an instance of the Repository Container// remember this can be a singleton if you// declare it early on and reuse it for the// lifetime of your applicationvar repo = new RepositoryContainer();// declare several instances of the Customer entity// and populate them with the values// this is only for demonstration purposes// in real life the list would come from a query// or perhaps from a collection of entities that// your system had already loaded beforehandvar customer1 = new Customer{ Id = 1, Name = "John", LastName = "Doe"};var customer2 = new Customer{ Id = 2, Name = "Jane", LastName = "Smith"};// declare a list and add the customersvar customers = new List<Customer>{ customer1, customer2};try{ // call the delete method and pass the list of customers repo.AppDb.Sales.Customer.Delete(customers);}catch (Exception ex){ // there was an error}Delete Where#
This is perhaps one of the most powerful features of The Sharp Factory Framework.
It gives you the ability to delete all entities within a given query. This is a feature lacking in some of the most popular ORMs in the market.
Three steps are required:
-
First you need an instance of the Query class.
-
Second add your predicate and craft your query.
-
Third call the Delete method in the correct repository and pass the query instance.
The following sample of C# code shows how to delete "all Customers where Balance > 0 or (CreatedDate = today and (status = "new" or status = "inactive"))":
Fluent Syntax#
// declare an instance of the Repository Container// remember this can be a singleton if you// declare it early on and reuse it for the// lifetime of your applicationvar repo = new RepositoryContainer();// declare an instance of Query and// specify the entity's property container// add the predicate to apply to the queryvar query = new Query<CustomerProperty>() .BeginPredicate() .Where(CustomerProperty.Balance).GreaterThan(0) .Or((exp) => { exp .Where(CustomerProperty.CreatedDate).EqualsDate(DateTime.Now) .Where((exp2) => { exp2 .Where(CustomerProperty.Status).Equals("new") .Or(CustomerProperty.Status).Equals("inactive"); }); }) .EndPredicate();// call the delete method and pass the query instancevar numberOfDeletedRecords = repo.AppDb.Sales.Customer.Delete(query);Builder Syntax#
// declare an instance of the Repository Container// remember this can be a singleton if you// declare it early on and reuse it for the// lifetime of your applicationvar repo = new RepositoryContainer();// declare an instance of Query and// specify the entity's property container// add the predicate to apply to the queryvar query = new Query<CustomerProperty>() .Builder((builder) => { builder .Predicate((predicate) => { predicate .Where(CustomerProperty.Balance).GreaterThan(0) .Or((exp) => { exp .Where(CustomerProperty.CreatedDate).EqualsDate(DateTime.Now) .Where((exp2) => { exp2 .Where(CustomerProperty.Status).Equals("new") .Or(CustomerProperty.Status).Equals("inactive"); }); }); }); });// call the delete method and pass the query instancevar numberOfDeletedRecords = repo.AppDb.Sales.Customer.Delete(query);