hillary
hillary

Reputation: 219

updating table in entity framework 4 /mvc 3!

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

           }
       }

Upvotes: 1

Views: 2022

Answers (3)

Jose Rojas
Jose Rojas

Reputation: 11

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

Upvotes: 1

Nix
Nix

Reputation: 58522

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

Upvotes: 1

ysrb
ysrb

Reputation: 6740

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.

Upvotes: 1

Related Questions