title: ASP.NET C# Entity Framework - How to update Foreign Key properly? - Part 2
body: <p>This question is like a Part 2 of my <a href="https://stackoverflow.com/questions/12373899/asp-net-c-sharp-entity-framework-how-to-update-foreign-key-properly">previous one here!</a></p>
  
  <p>Here are things I learned and hoping to be true:</p>
  
  <ol>
  <li>Do not use Data Entity Context as Static</li>
  <li>Dispose the data context after used -usually after one time-</li>
  <li>Pass parameters into UPDATING Entity method instead of the Entity
  (which I'll explain and in here my crossroad lies)</li>
  </ol>
  
  <p>Here is my modified code:</p>
  
  <pre><code>public class ManagerBase
      {
          private NoxonEntities _entities = null;
  
          public NoxonEntities Entities
          {
              get
              {
                  if (_entities == null)
                      _entities = new NoxonEntities();
  
                  return _entities;
              }
          }
      }
  
  //Managers uses ManagerBase class as a Base class
  MemberManager currentMemberManager = new MemberManager();
  currentMemberManager.Save(memberId, username, password, languageId);
  
  
  //MemberManager class
  public Member Save(long memId, string username, string password, int langId)
          {
              using (var Context =   base.Entities)
              {
                  var Data = Context.Member.First(c =&gt; c.Id == memId);
  
                  Data.Username = username;
                  Data.Password = password;
                  Data.Language = Context.Language.First(c =&gt; c.Id == langId); //Gets the foreign entity
  
                  Context.SaveChanges();
  
                  return Data;
              }
          }
  </code></pre>
  
  <p>This works without exception. But normally this <strong>SAVE</strong> method is an implemented method by an <strong>interface</strong>. So, I'd rather to pass an <strong>OBJECT</strong> value to the SAVE method than <strong>passing all the fields</strong> like above.
  For example I'd like to use this:</p>
  
  <pre><code>//Member is an EntityFramework Object which was created during EF Model when I added by visual studio
  //Filter is the method that returns an EntityFramework Object from EF (and eventually database) with given some arguments
  //SomeArguments could be  MemberId = 2
  Member modifiedMember = currentMemberManager.Filter(someArguments);
  modifiedMember.UserName = "newvalue";
  currentMemberManager.Save(modifiedMember);
  </code></pre>
  
  <p>In the SAVE method perhaps I could use like this:</p>
  
  <pre><code>public Member Save(Member modifiedMember)
              {
                  using (var Context =   base.Entities)
                  {
                      var Data = Context.Member.First(c =&gt; c.Id == modifiedMember.Id);
  
                      Data.Username = modifiedMember.Username;
                      Data.Password = modifiedMember.Password;
                      Data.Language = Context.Language.First(c =&gt; c.Id == modifiedMember.LanguageId); //Gets the foreign entity
  
                      Context.SaveChanges();
  
                      return Data;
                  }
              }
  </code></pre>
  
  <p>If I am gonna use this just like the one right above, I think I should NOT use the passing object EF Object and instead perhaps I should use some other class that I'll write just to map the parameters to the EF Member Entity.</p>
  
  <p><strong>First question: Is this a good approach?</strong></p>
  
  <pre><code>public class MyMember
  {
      public long Id { set; get; }
      public string Username { set; get; }
      public string Password { set; get; }
      public int LanguageId { set; get; }
  }
  
  MyMember.Username = "NewValue";
  MyMember.LanguageId = 4; //4 as in new value
  currentMemberManager.Save(MyMember); //As you can see MyMember is not an EF Entity in here
  </code></pre>
  
  <p>But this will take long time since there is already some written code.</p>
  
  <p><strong>Second question: Can't I use an EF Entity object <em>OUTSIDE</em> and modify it there to save it <em>IN</em> the <em>MemberManager</em> class?</strong></p>
  
  <p>Again, thank you very much for your help in advance.</p>