In my solution I have a Domain project and a DataAccess project. My Domain project contains domain objects and interfaces for various repositories and an interface for a UnitOfWork which will contain all my repositories. My DataAccess project contains the implementations of all these interfaces and the EF versions of my Domain objects.
So if I have a class called Person
and I want to add a Person
to the DB it would look like this:
unitOfWork.People.Add(person);
unitOfWork.Complete();
Inside unitOfWork.People.Add(person)
I map my domain Person
object to my EF Person
object with AutoMapper. Then I add my EF Person
object to the EF DbContext
object.
public void Add(Person person) {
DataAccess.Entities.Person dbPerson = Mapper.Map<DataAccess.Entities.Person>(person);
dbContext.People.Add(dbPerson);
}
The Complete() function just wraps SaveChanges().
public int Complete() {
dbContext.SaveChanges();
}
Normally EF updates an inserted object's DB generated ID after insertion. But in this case EF knows nothing about my Domain Person object.
Since my Domain Person object doesn't have it's ID, I can't update the DB entry or re-fetch it from the database.
The only alternative I can think of is generating a guid on the server and using that as an ID. But this creates redundancy in the form of two IDs. It also isn't very user friendly since users will reference this ID in search fields. Remembering ID 135 is much simpler than remembering a guid.
Is there some way to get dbContext to update the domain object's ID directly or alternatively have the ID bubble up through the layers?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…