更新实体框架 4 /mvc 3 中的表!

发布于 2024-10-31 11:59:44 字数 1043 浏览 1 评论 0原文

你能帮忙吗?我敢打赌这不是什么难事……但我是 EF 的新手,面临着周末的截止日期。我想用值更新表..但主键是标识列。所以我的任务是这样的..如果存在,则更新..如果它没有添加到
表..这是我的代码..并且我陷入了其他部分..!

表结构如下

主键表 - System: 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();

           }
       }

could you help with this? I bet this isn't any tough one..but am new to EF and facing a weekend deadline. I want to update a table with values.. but the primary key is identity column. So my task is like this.. if it exists, update.. if it doesn't add to the
table.. this is my code..and am stuck in this else part..!

Table structure is like this

Primary Key table - System: SystemId, SystemName

Foreign Key table - 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();

           }
       }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

奢华的一滴泪 2024-11-07 11:59:44

试试这个:

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

编辑。它应该是配置而不是系统配置。

Try this:

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

Edit. It should be config not systemConfig.

纸短情长 2024-11-07 11:59:44

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

这会起作用:

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

The ApplyCurrentValues method will apply scalar attributes to an entity that matches the same key. My assumption is that you are modifying a real entity (an object that has a valid entity key).

This would work:

var eSet  = config.EntityKey.EntitySetName;
Context.ApplyCurrentValues(eSet, systemConfig);
Context.SaveChanges();
半夏半凉 2024-11-07 11:59:44
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();
    }
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();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文