Код: Выделить всё
using System.Text.Json;
namespace CargoHub
{
public class DataMigration
{
private readonly AppDbContext _context;
public DataMigration(AppDbContext dbContext)
{
_context = dbContext;
}
public async Task MigrateData(string folder = "../Data")
{
var processedFiles = new List();
var jsonFiles = Directory.GetFiles(folder);
foreach (var jsonFile in jsonFiles)
{
try
{
await ProcessFile(jsonFile);
processedFiles.Add(jsonFile);
}
catch (Exception ex)
{
Console.WriteLine($"Error processing {jsonFile}: {ex.Message}");
}
}
return processedFiles;
}
private async Task ProcessFile(string filePath)
{
string fileName = Path.GetFileNameWithoutExtension(filePath);
switch (fileName)
{
case "clients":
var clients = await DeserializeJson(filePath);
await SaveToDatabase(clients);
break;
// Uncomment to handle more file types
// case "inventories":
// var inventories = await DeserializeJson(filePath);
// await SaveToDatabase(inventories);
// break;
case "item_groups":
var itemGroups = await DeserializeJson(filePath);
await SaveToDatabase(itemGroups);
break;
}
}
private async Task DeserializeJson(string filePath)
{
try
{
var jsonData = await File.ReadAllTextAsync(filePath);
return JsonSerializer.Deserialize(jsonData)!;
}
catch (Exception ex)
{
Console.WriteLine($"Error deserializing {filePath}: {ex.Message}");
return new List();
}
}
private async Task SaveToDatabase(List entities) where T : class
{
if (entities.Count == 0) return;
_context.AddRange(entities);
int changedRows = await _context.SaveChangesAsync();
Console.WriteLine(changedRows);
}
}
}
Код: Выделить всё
using System.Text.Json.Serialization;
namespace CargoHub
{
public class Client : BaseModel
{
[JsonPropertyName("id")]
public int Id { get; set; }
[JsonPropertyName("name")]
public string? Name { get; set; }
[JsonPropertyName("address")]
public string? Address { get; set; }
[JsonPropertyName("city")]
public string? City { get; set; }
[JsonPropertyName("zip_code")]
public string? ZipCode { get; set; }
[JsonPropertyName("province")]
public string? Province { get; set; }
[JsonPropertyName("country")]
public string? Country { get; set; }
[JsonPropertyName("contact_name")]
public string? ContactName { get; set; }
[JsonPropertyName("contact_phone")]
public string? ContactPhone { get; set; }
[JsonPropertyName("contact_email")]
public string? ContactEmail { get; set; }
}
}
Код: Выделить всё
namespace CargoHub
{
public class BaseModel
{
[JsonPropertyName("created_at")]
public DateTime? CreatedAt { get; set; }
[JsonPropertyName("updated_at")]
public DateTime? UpdatedAt { get; set; }
}
}
Код: Выделить всё
[
{
"id": 1,
"name": "Raymond Inc",
"address": "1296 Daniel Road Apt. 349",
"city": "Pierceview",
"zip_code": "28301",
"province": "Colorado",
"country": "United States",
"contact_name": "Bryan Clark",
"contact_phone": "242.732.3483x2573",
"contact_email": "robertcharles@example.net",
"created_at": "2010-04-28 02:22:53",
"updated_at": "2022-02-09 20:22:35"
},
{
"id": 2,
"name": "Williams Ltd",
"address": "2989 Flores Turnpike Suite 012",
"city": "Lake Steve",
"zip_code": "08092",
"province": "Arkansas",
"country": "United States",
"contact_name": "Megan Hayden",
"contact_phone": "8892853366",
"contact_email": "qortega@example.net",
"created_at": "1973-02-24 07:36:32",
"updated_at": "2014-06-20 17:46:19"
}
]
Несмотря на то, что я назвал все свойства в своем классе Client в соответствии со структурой JSON, я все равно сталкиваюсь с проблемами при десериализации данных. В частности, некоторые поля заполняются неправильно.
Я уже пробовал удалить атрибуты [JsonPropertyName], но проблема не устранена.
Что может быть? Меня не хватает в процессе десериализации, или с моим подходом что-то еще не так?
Буду признателен за любую помощь!
Подробнее здесь: https://stackoverflow.com/questions/792 ... alizing-da
Мобильная версия