Update
Table Of Contents
Update Method
To update entities in the data store you will need to call the Update 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}.Update
The Update method has four overloads:
-
The first one takes an entity to be updated.
This overload returns a boolean. True if succeeded, false if no changes were found before the update.
Throws and exception if the entity passed in is null.
Throws and exception if the entity is not found in the database.
-
The second overload takes a list of entities and returns void.
Throws an exception if the list passed in is null.
Throws an exception if any entity in the list is null.
Throws an exception if any entity in the list is not found in the database.
-
The third overload takes two entities. Compares the two for changes. Only updates if changes are found.
-
The first parameter is the original(unchanged) entity.
-
The second parameter is the changed entity.
This overload returns a boolean. True if succeeded, false if no changes were found.
The entities will be compared and the database will be called only if the entities are different.
Once the update is finished the entities will be merged so that they are the same.
Throws an exception if any of the two entities passed in is null.
-
-
The fourth overload takes an instance of the Query class.
This overload returns a List of entities that corresponds to the query passed in.
This is one of the most powerful features available.
Any property that is generated at the database like Identity will be populated after calling Update.
Update Single
The following sample of C# code shows how to update a Customer entity in the database:
Update List
To update a list you can just pass the List
The following sample of C# code shows how to update a list of customer in the database:
Update Change Tracking
If you are tracking the changes to an entity you can pass the original(unchanged) entity and the changed entity when calling the Update method.
The following sample of C# code shows how to update a Customer in the database using this feature:
Update Where
This is perhaps one of the most powerful features of The Sharp Factory Framework.
It gives you the ability to update all entities within a given query. This is a feature lacking in some of the most popular ORMs in the market.
Five steps are required:
-
First you need an instance of the Query class.
-
Second call .UpdateValues() on the Query instance to make the .Set pipeline available.
-
Third call .Set() for each property you want to update.
The Set method takes two parameters
-
Property Enum for example Customer.Status.
-
Value the value to set to the property.
-
-
Fourth add your predicate and craft the rest of your query.
-
Fifth call the Update method on the correct repository and pass the query instance.
The following sample of C# code shows how to "set status equals "active" and LastModified equals "now" to all Customers where Balance > 0 or (CreatedDate = today and (status = "new" or status = "inactive"))":