Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I want to read json file data with specific json object array and insert bulk data into database in Asp.net Core.

I have two tables in Databse like naming as M203StructureKind, FlhDivision
SQL
CREATE TABLE [dbo].[M203StructureKind](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Code] [nvarchar](1) NOT NULL,
    [Description] [nvarchar](30) NOT NULL,
    [AgencyId] [int] NOT NULL,
    [IsActive] [bit] NOT NULL,
    [IsDeleted] [bit] NOT NULL,
    [AddedBy] [int] NOT NULL,
    [AddedOn] [datetime] NOT NULL,
    [ModifiedBy] [int] NOT NULL,
    [ModifiedOn] [datetime] NOT NULL
)

CREATE TABLE [dbo].[FlhDivision](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Code] [nvarchar](1) NOT NULL,
    [Description] [nvarchar](15) NOT NULL,
    [AgencyId] [int] NOT NULL,
    [IsActive] [bit] NOT NULL,
    [IsDeleted] [bit] NOT NULL,
    [AddedBy] [int] NOT NULL,
    [AddedOn] [datetime] NOT NULL,
    [ModifiedBy] [int] NOT NULL,
    [ModifiedOn] [datetime] NOT NULL
)

Also I have generated entities from database using asp.net core in models folder

like M203StructureKind.cs
C#
public partial class M203structureKind
{
    public int Id { get; set; }
    public string Code { get; set; }
    public string Description { get; set; }
    public int AgencyId { get; set; }
    public bool? IsActive { get; set; }
    public bool IsDeleted { get; set; }
    public int AddedBy { get; set; }
    public DateTime AddedOn { get; set; }
    public int ModifiedBy { get; set; }
    public DateTime ModifiedOn { get; set; }
}

and FlhDivision.cs
C#
public partial class FlhDivision
{
    public int Id { get; set; }
    public string Code { get; set; }
    public string Description { get; set; }
    public int AgencyId { get; set; }
    public bool? IsActive { get; set; }
    public bool IsDeleted { get; set; }
    public int AddedBy { get; set; }
    public DateTime AddedOn { get; set; }
    public int ModifiedBy { get; set; }
    public DateTime ModifiedOn { get; set; }
}

Now I Have MasterData.json file which contains this two tables data with json object.

MasterData.json file contains following contains like
JavaScript
{ 
    "M203StructureKind": [
        {
            "Code": "1",
            "Description": "BRIDGE"
        },
        {
            "Code": "2",
            "Description": "CULVERT"
        },
        {
            "Code": "3",
            "Description": "TUNNEL"
        },
        {
            "Code": "4",
            "Description": "TRAIL BRIDGE"
        },
        {
            "Code": "5",
            "Description": "OTHER"
        }
    ],
    "FlhDivision": [
        {
            "Code": 1,
            "Description": "CFL"
        },
        {
            "Code": 2,
            "Description": "EFL"
        },
        {
            "Code": 3,
            "Description": "WFL"
        }
    ]
}

So, Now I want to insert bulk data into table using POST Api in Asp.net Core. But using foreach loop this is too much lengthy process also time consuming because I have so many tables to insert bulk data into database. So tell me the solution

What I have tried:

C#
[HttpPut("copyMasterData/{id}")]
public async Task<IActionResult> CopyMasterData(int id, bool isCopiedData)
{
    try
    {
        string contentRootPath = _appSettings.MasterJsonPath;
        
        var folderDetails = Path.Combine(Directory.GetCurrentDirectory(), $"wwwroot\\{"MasterData\\masterdata.json"}");
        var JSON = System.IO.File.ReadAllText(folderDetails);
        dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(JSON);
        var response = 0;
        foreach (JObject structKind in jsonObj["M203StructureKind"])
        {
            string code = (string)structKind["Code"];
            string description = (string)structKind["Description"];
            int agencyId = id;
            bool IsActive = true;
            bool IsDeleted= false;
            int AddedBy = User.GetUserId();
            int ModifiedBy = User.GetUserId();
            DateTime ModifiedOn = DateTime.Now;
            DateTime AddedOn = DateTime.Now;
            response = await _m203structkindService.AddAsync(structKind.ToSingle<M203structureKind>());
        }

        foreach (JObject flh in jsonObj["FlhDivision"])
        {
            string code = (string)flh["Code"];
            string description = (string)flh["Description"];
            int agencyId = id;
            bool IsActive = true;
            bool IsDeleted = false;
            int AddedBy = User.GetUserId();
            int ModifiedBy = User.GetUserId();
            DateTime ModifiedOn = DateTime.Now;
            DateTime AddedOn = DateTime.Now;
            response = await _flhdivisionService.AddAsync(flh.ToSingle<FlhDivision>());
        }             

        return response > 0 ? Ok(new { message = "Master data added successfully." }) : StatusCode(500, "An error occured while adding master data. Please try again...");
    }
    catch (Exception ex)
    {
        return StatusCode(500, ResponseMessages.GetErrorMessage(ResponseMessages.MessageType.Add, ex.Message));
    }
}
Posted
Updated 15-Oct-19 7:01am
v2
Comments
RedDk 15-Oct-19 13:33pm    
Weeel,

Here's whats shaking. This is the third repost of the problem. But I'm giving you credit for doing something. Even though creating code for CREATE TABLE isn't in the realm of JSON. So much as it's in the realm C#.

Anyway, to keep a thread alive here @ QA, REPLY to a comment. Or use the SEARCH by typing in a few of the words that each comment contains ... perhaps the ones you deem most salient

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900