I have a complete separation of my Entity Framework objects and my POCO objects, I just translate them back and forth...
i.e:
// poco
public class Author
{
public Guid Id { get; set; }
public string UserName { get; set; }
}
and then I have an EF object "Authors" with the same properties..
So I have my business object
var author = new Author { UserName="foo", Id="Guid thats in the db" };
and I want to save this object so I do the following:
var dbAuthor = new dbAuthor { Id=author.Id, UserName=author.UserName };
entities.Attach(dbAuthor);
entities.SaveChanges();
but this gives me the following error:
An object with a null EntityKey value
cannot be attached to an object
context.
EDIT:
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?
I would hope I'd be able to do this, as hard coded strings killing off compile time validation would suck =)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…