Entity Framework not updating record

Viewed 38

I have this method in the ParametersController class:

        public string SaveParameter(int id, string param, string val)
    {
        ProjectConfigurations config = new ProjectConfigurations();
        ProjectConfigurationsDAO dao = new ProjectConfigurationsDAO();

        config = dao.GetProjectConfigurationsByProjectAndParameter(id, param);
        
        config.Value = val;

        dao.Edit(config);

        return JsonConvert.SerializeObject(new { success = true, data = config });
        //return JsonConvert.SerializeObject(config);
    }

In my BaseDao class, I have this:

        public void Edit(T entity)
    {
        using (context = new Context())
        {
            context.Entry<T>(entity).State = EntityState.Modified;
            context.SaveChanges();
        }
    }

The record is not updated. I'm new to EF, what I'm I doing wrong?

1 Answers

never try to create any generic repository since they are uselless. Try this

    public string SaveParameter(int id, string param, string val)
    {
        using (context = new Context())
        {
         var existedConfig=conext.Set<Config>.FirstOrDefault(i=> i.Id==id);
         existedConfig.Value=val;
          context.Entry<Config>(existedConfig).State = EntityState.Modified;
         context.SaveChanges();
         return JsonConvert.SerializeObject(new { success = true, data = config });
        }
Related