2011-04-11 5 views
1

도와 드릴까요? 나는 이것이 결코 힘든 하나는 아니지만 EF에 새로 왔고 주말 기한에 직면했을 것이라고 확신합니다. 값으로 테이블을 업데이트하려고하지만 기본 키는 ID 열입니다. 그래서 내 작업은 이것과 같습니다 .. 존재한다면, 업데이트 ..
테이블에 추가하지 않으면 .. 이것은 내 코드입니다. 그리고 다른 부분에 붙어 있습니다! , 시스템 식별자 SystemName을엔티티 프레임 워크 4/mvc 3의 테이블 업데이트 중!

외래 키 테이블 -에서 SystemConfiguration : 시스템 -

테이블 구조는

기본 키 테이블처럼 SystemConfigurationId, 시스템 식별자, SystemRAM, SystemHard 디스크

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

      } 
     } 

답변

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

편집. systemConfig가 아닌 config이어야합니다.

+0

인텔리는 .Attibute을 제공하지 않습니다! – hillary

+0

예. . 속성은 예제 일뿐입니다. .Attribute를 업데이트 할 특성으로 바꾸십시오. 예 : config.DateModified = DateTime.Now; 등 – ysrb

+0

감사합니다 .. ysrb ..이 매력처럼 작동 ..! 더 많은 솔루션을 제공한다면 더 많은 연구가 필요합니다. 수작업으로 많은 열을 설정하는 것보다 ... – hillary

1

ApplyCurrentValues 메서드는 동일한 키와 일치하는 엔티티에 스칼라 특성을 적용합니다. 내 가정은 실제 엔터티 (유효한 엔터티 키가있는 개체)를 수정하는 것입니다.

이 작동합니다 :

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

이것은 무엇을 의미합니까? EntityKey 키 = 새로운 EntityKey ("Context.SystemConfigurations", "SystemConfigurationId", config.SystemConfigurationId); var eSet = key.EntitySetName; Context.ApplyCurrentValues ​​(eSet, systemConfig); 하지만 아니, 이건 작동하지 않습니다. – hillary

+0

내가 systemConfig.SystemConfigurationId = key.value를 설정할 때까지는 작동하지 않습니다.하지만 왜 수동으로해야합니까?! 내가 놓치고있다! – hillary

+0

systemConfig의 출처에 대한 배경을 좀 더 알려 주면 내 게시물을 업데이트하고 좀 더 "현대적인"솔루션을 제공 할 것입니다. – Nix

1
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(); 
    } 
관련 문제