TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Adhikar Patil
NA
481
129.1k
read json file data and insert bulk data
Oct 15 2019 11:43 AM
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
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
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
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
{
"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
I have tried using foreach but this is not proper solution
<pre>[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));
}
}</pre>
Tell me the bulk insert with Json Data in Asp.net core POST Api .
Reply
Answers (
2
)
Microsoft .NET Framework must support c# 8 - PROTEST PAGE
Routing in ASP.Net MVC - Default