Create

Table Of Contents

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:

  1. 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.

  2. 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:

C#
// 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 application

var repo = new RepositoryContainer();

// create an instance of the Customer entity

// and populate the values


var customer = new Customer
{
    Name = "John",
    LastName = "Doe"
};

// call the create method and pass the new customer

var 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 to the Create method.

The following sample of C# code shows how to create a list of customer in the database:

C#
// 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 application

var repo = new RepositoryContainer();

// declare several instances of the Customer entity

// and populate them with the values


var customer1 = new Customer
{
    Name = "John",
    LastName = "Doe"
};

var customer2 = new Customer
{
    Name = "Jane",
    LastName = "Smith"
};

// declare a list and add the customers

var 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

}