Home > OS >  Can't save data In database
Can't save data In database

Time:10-26

I'm getting A JSON file from another server, deserializing it and trying to add in my C# objects and then save it in my DB. The problem is that I'm not able to save this data. I'm not getting any kind of error and I'm not sure what I'm doing wrong. Here is my code :

public class GameRepository : IGameRepository
{
     private readonly GameDbContext _context;
     //private readonly IMapper<Game, GameModel> _gameMapper;
     public GameRepository(GameDbContext context)
     {
         _context = context;
     }
}
string catJson;
string gameJson;

using (WebClient _web = new WebClient())
{
    catJson = _web.DownloadString(_catUri);
}

var gamecatss = JsonConvert.DeserializeObject<List<Category>>(catJson);

using (WebClient _web = new WebClient())
{
    gameJson = _web.DownloadString(_uri);
}

var games = JsonConvert.DeserializeObject<List<Games>>(gameJson);
List<Games> gameModels = new List<Games>();
gameModels.AddRange(games);
List<Category> categoryModels = new List<Category>();
categoryModels.AddRange(gamecatss);
_context.SaveChanges();
return JsonConvert.SerializeObject(games);`

Here is my DbContext:

public partial class GameDbContext : DbContext
{
    public GameDbContext()
    {
    }

    public GameDbContext(DbContextOptions<GameDbContext> options) : 
 base(options)
    {
    }

    public virtual DbSet<Category> Category { get; set; }
    public virtual DbSet<Games> Games { get; set; }`

This is my repository:

public class GameRepository : IGameRepository 
{ 
    private readonly GameDbContext _context; 
    //private readonly IMapper<Game, GameModel> _gameMapper; 
    public GameRepository(GameDbContext context) { _context = context; }
}
    public partial class Games
        {
            public int Id { get; set; }
            public string System { get; set; }
            public string SubSystem { get; set; }
            public string PageCode { get; set; }
            public string LowRtpUrl { get; set; }
            public string LowRtpMobileUrl { get; set; }
            public string LowRtpUrlExternal { get; set; }
            public string LowRtpMobileUrlExternal { get; set; }
            public string MobilePageCode { get; set; }
            public string MobileAndroidPageCode { get; set; }
            public string MobileWindowsPageCode { get; set; }
            public string CustomSortType { get; set; }
            public string ImageUrl { get; set; }
}

Id is PK here. category class looks similar. P.S return works fine also it displays data correctly.

With one look it should work, my Game class and category classes are fine. I'm sure, I scaffolded them, any ideas what I'm doing wrong?`

CodePudding user response:

You need something like this

var games = JsonConvert.DeserializeObject<List<Games>>(gameJson);
var gamecatss = JsonConvert.DeserializeObject<List<Category>>(catJson);

// remove Ids
games.ForEach(x => x.Id = 0);
gamecatss.ForEach(x => x.Id = 0);

_context.Games.AddRange(games);
_context.Category.AddRange(gamecatss);

_context.SaveChanges();

CodePudding user response:

So finally I found out what I was doing wrong

var games = JsonConvert.DeserializeObject<List<Games>>(gameJson);
var gamecatss = JsonConvert.DeserializeObject<List<Category>>(catJson);
List<GamesDTO> gameModels = new List<GamesDTO>()  
List<CategoryDTO> categoryModels = new List<CategoryDTO>();
_context.Games.AddRange(games);
_context.Category.AddRange(gamecatss);
_context.SaveChanges();

I did mapping in forech loop to map from DTO to game model and then saved and it worked fine.

  • Related