title: How can I attach an Entity Framework object that isn't from the database?
body: <p>I have a complete separation of my Entity Framework objects and my POCO objects, I just translate them back and forth...</p>
  
  <p>i.e:</p>
  
  <pre><code>// poco
  public class Author
  {
     public Guid Id { get; set; }
     public string UserName { get; set; }
  }
  </code></pre>
  
  <p>and then I have an EF object "Authors" with the same properties..</p>
  
  <p>So I have my business object</p>
  
  <pre><code>var author = new Author { UserName="foo", Id="Guid thats in the db" };
  </code></pre>
  
  <p>and I want to save this object so I do the following:</p>
  
  <pre><code>var dbAuthor = new dbAuthor { Id=author.Id, UserName=author.UserName };
  entities.Attach(dbAuthor);
  entities.SaveChanges();
  </code></pre>
  
  <p>but this gives me the following error:</p>
  
  <blockquote>
    <p>An object with a null EntityKey value
    cannot be attached to an object
    context.</p>
  </blockquote>
  
  <p><strong>EDIT:</strong>
  It looks like I have to use entities.AttachTo("Authors", dbAuthor); to attach without an EntityKey, but then I have hard coded magic strings, which will break if I change my entity set names at all and I wont have any compile time checking... Is there a way I can attach that keeps compile time checking?</p>
  
  <p>I would hope I'd be able to do this, as hard coded strings killing off compile time validation would suck =)</p>