【问题标题】:updating table in entity framework 4 /mvc 3!更新实体框架 4 /mvc 3 中的表!
【发布时间】:2011-04-11 01:14:44
【问题描述】:

你能帮忙吗?我敢打赌,这不是什么难事……但对 EF 来说是新手,并且面临周末的最后期限。我想用值更新一个表。但主键是标识列。所以我的任务是这样的..如果它存在,更新..如果它没有添加到
表..这是我的代码..我被困在这个其他部分..!

表结构是这样的

主键表 - 系统:SystemId、SystemName

外键表-SystemConfiguration:SystemConfigurationId、SystemId、SystemRAM、SystemHard-Disk

        public void SaveSystemConfigurations(SystemConfiguration systemConfig)
         {
              var config = (from s in Context.SystemConfiguration 
                       where s.SystemId == systemConfig.SystemId
                            select s).FirstOrDefault();

             if (config == null)
             {
                 Context.SystemConfigurations.AddObject(systemConfig);
                 Context.SaveChanges();
             }
             else
             {
                // EntityKey systemConfigKey= new EntityKey("systemConfig", "systemConfigId", config.SystemConfigurationId);
                 Context.SystemConfigurations.Attach(systemConfig);
                Context.SaveChanges();

           }
       }

【问题讨论】:

    标签: entity-framework asp.net-mvc-3 entity-framework-4


    【解决方案1】:

    试试这个:

            public void SaveSystemConfigurations(SystemConfiguration systemConfig)
             {
                  var config = (from s in Context.SystemConfiguration 
                           where s.SystemId == systemConfig.SystemId
                                select s).FirstOrDefault();
    
                 if (config == null)
                 {
                     Context.SystemConfigurations.AddObject(systemConfig);
                 }
                 else
                 {
                    config.Attribute = value; // Do your update here
    
               }
               Context.SaveChanges();
           }
    

    编辑。应该是 config 而不是 systemConfig。

    【讨论】:

    • 智能感知不提供任何 .Attibute?!
    • 是的。 .Attribute 只是一个例子。将 .Attribute 替换为要更新的属性。例如。 config.DateModified = DateTime.Now;等
    • 谢谢.. ysrb..这就像一个魅力..!我正在进一步研究是否有更好的解决方案..而不是手动设置这么多列..
    • @hillary 你想做什么?您不需要设置很多列,因为配置将从数据库中填充属性。
    • ysrb .. 这就是我的意思.. 通过手动设置每一列。否则 { config.SystemIsBlueToothEnabled= systemConfig.SystemIsBlueToothEnabled; config.SystemCPU=系统配置.SystemCPU; config.SystemMaxRam=系统配置.SystemMaxRam; } 这不是你的意思吗?
    【解决方案2】:

    ApplyCurrentValues 方法会将标量属性应用于匹配相同键的实体。我的假设是您正在修改一个真实的实体(一个具有有效实体键的对象)。

    这可行:

    var eSet  = config.EntityKey.EntitySetName;
    Context.ApplyCurrentValues(eSet, systemConfig);
    Context.SaveChanges();
    

    【讨论】:

    • 这是你的意思吗? EntityKey key= new EntityKey("Context.SystemConfigurations","SystemConfigurationId", config.SystemConfigurationId); var eSet = key.EntitySetName; Context.ApplyCurrentValues(eSet, systemConfig);但不,这不起作用..
    • 在我设置 systemConfig.SystemConfigurationId = key.value.. 之前不会工作。但为什么我们必须手动执行呢?!我错过了什么吗!
    • 如果你给我更多关于 systemConfig 来自哪里的背景知识,我会更新我的帖子并给你一个更“现代”的解决方案。
    【解决方案3】:
    public void SaveSystemConfigurations(SystemConfiguration systemConfig)
         {    
        var context = new EntitiesModel(); 
        //here is the name of the partial class created on the Context area of the edmx designer.cs
        var config = (from s in context.SystemConfiguration 
                       where s.SystemId == systemConfig.SystemId
                            select s).FirstOrDefault();
        context.ApplyCurrentValues(config.EntityKey.EntitySetName, systemConfig);
        // systemConfig comes from the view with values set by the user
        // you dont have to manually need to map one value at the time
        context.SaveChanges();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-10
      • 1970-01-01
      • 2014-09-07
      • 1970-01-01
      • 1970-01-01
      • 2011-11-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多