Create
Insert one entity or a list of entities with the Create methods.
On this page
Create Method#
To create entities in the data store you will need to call the Create 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}.Create
The Create method has two overloads:
-
The first one takes an entity to be created.
This overload returns a boolean. True if succeeded, otherwise false.
Throws an exception if the value passed in is null.
-
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 it fails to create one entity.
Throws an exception if any entity in the list is null.
Any property that is generated at the database like Identity will be populated after calling Create.
Create Single#
The following sample of C# code shows how to create a Customer entity in the database:
// create 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();// create an instance of the Customer entity// and populate the valuesvar customer = new Customer{ Name = "John", LastName = "Doe"};// call the create method and pass the new customervar success = _repo.AppDb.Sales.Customer.Create(customer);if (success){ //get the value of auto generated columns var identity = customer.Id; var createdDate = customer.CreatedDate;}Create List#
To create a list you can just pass a List<Customer> to the Create method.
The following sample of C# code shows how to create a list of customer in the database:
// create 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 valuesvar customer1 = new Customer{ Name = "John", LastName = "Doe"};var customer2 = new Customer{ Name = "Jane", LastName = "Smith"};// declare a list and add the customersvar customers = new List<Customer>{ customer1, customer2};try{ // call the create method and pass the list of customers _repo.AppDb.Sales.Customer.Create(customers);}catch (Exception ex){ // there was an error}