Create multiple records in database with 1 insert

I have a current application that allows user to create 2 record with a single insert.

If customer number 0001 has 1 type of service, it creates a record in the database
0001 Service1,…,…
If customer number 0002 has 2 types of service, it creates 2 record in the database
0002 Service1, …,…
0002 Service2, …,…

Is there any way to do the same in Radzen?
i need to keep some information separately as service1 and service2 differs i.e. enrolment date might be different

Hi @kegler,

By default Radzen generated application will perform single record add on POST. Here is how it looks usually the POST method:

    partial void OnProductCreated(Models.Northwind.Product item);

    [HttpPost]
    public IActionResult Post([FromBody] Models.Northwind.Product item)
    {
        if (item == null)
        {
            return BadRequest();
        }

        this.OnProductCreated(item);
        this.context.Products.Add(item);
        this.context.SaveChanges();

        var key = item.ProductID;
        var itemToReturn = this.context.Products
            .Where(i => i.ProductID == key)
            .Include(i => i.Supplier)
            .Include(i => i.Category)
            .FirstOrDefault();

        return new JsonResult(itemToReturn, new Newtonsoft.Json.JsonSerializerSettings
        {
            ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
        })
        {
            StatusCode = 201
        };
    }

You can use OnXXXCreated partial method however to extend the default behavior:

public partial class ProductsController
{
    partial void OnProductCreated(Models.Northwind.Product item)
    {
        // Your code goes here
    }
}

Other possible solution is to chain the insert method calls in Radzen, i.e. execute additional insert on insert then:

Best Regards,
Vladimir

1 Like