简体   繁体   English

实体框架不更新记录

[英]Entity Framework not updating record

I have this method in the ParametersController class:我在 ParametersController 类中有这个方法:

        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:在我的 BaseDao 类中,我有这个:

        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?我是 EF 的新手,我做错了什么?

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 });
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM