Asynchronously Insert (Save) data in multiple tables using Entity Framework in ASP.Net Core MVC

Last Reply 18 days ago By dharmendr

Posted 19 days ago

Hi All,

I have a data as given below and this Data needs to be inserted in two tables using EF and using Async Task in ASP.Net Core.

1) Employee

2) Employee_Adress

Could anybody guide me with the sample code how to achieve this?

JsonData: 

{
  "EmployeeId": 1,
  "Name": "Bob",
  "Designation": "Manager",
  "Address": [
    {
      "AddressId": 1,
      "EmployeeId": 1,
      "Address": "Dollors Colony, US",
      "AddressType": "present"
    },
    {
      "AddressId": 2,
      "EmployeeId": 1,
      "Address": "Pound Colony, UK",
      "AddressType": "permanent"
    }
  ]
}

Thanks & Regards,

Sreenivas K

You are viewing reply posted by: dharmendr 18 days ago.
Posted 18 days ago Modified on 15 days ago

Hi skaushik,

Check this example. Now please take its reference and correct your code.

1. You need to create two classes as per the Columns in the Json string.

2. Read the Json string and convert it to class object.

3. Add the record to database using loop.

Namespaces

using System.IO;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Hosting;

Controller

public class HomeController : Controller
{
    private IHostingEnvironment Environment;
    public HomeController(IHostingEnvironment _environment)
    {
        Environment = _environment;
    }
    // GET: /Home/
    public async Task<IActionResult> Index()
    {
        string path = Path.Combine(this.Environment.WebRootPath, "Json.txt");
        string json = System.IO.File.ReadAllText(path);
        EmployeeDetails employeeDetails = JsonConvert.DeserializeObject<EmployeeDetails>(json);
        InsertEmployeeDetails(employeeDetails);
        return View();
    }

    public static async Task InsertEmployeeDetails(EmployeeDetails employeeDetails)
    {
        using (var db = new EmployeeEntities())
        {
            Employee emp = new Employee();
            emp.EmployeeId = employeeDetails.EmployeeId;
            emp.Name = employeeDetails.Name;
            emp.Designation = employeeDetails.Designation;
            // Add to Employee Table.
            db.Employees.Add(emp);

            foreach (AddressDetails address in employeeDetails.Address)
            {
                Contact contact = new Contact
                {
                    AddressId = address.AddressId,
                    EmployeeId = address.EmployeeId,
                    Address = address.Address,
                    AddressType = address.AddressType
                };
                // Add each Address to Contact Table.
                db.Contacts.Add(contact);
            }
            await db.SaveChangesAsync();
        }
    }

    public class EmployeeDetails
    {
        public int EmployeeId { get; set; }
        public string Name { get; set; }
        public string Designation { get; set; }
        public List<AddressDetails> Address { get; set; }
    }

    public class AddressDetails
    {
        public int AddressId { get; set; }
        public int EmployeeId { get; set; }
        public string Address { get; set; }
        public string AddressType { get; set; }
    }
}