Automapping to EntityKeys in Entity Framework
Does anyone have a technique to automap (using Automapper) references to child entities. So say I have a ViewModel:
class AddressModel
{
int Id;
string Street;
StateModel State;
}
class StateMod开发者_如何学编程el
{
int Id;
string Name;
}
And I pass this into a repository to map to equivalent entities in Entity Framework. When Automapping, I want it to automap AddressModel.State.ID to the EntityKey of AddressEntity.StateReference. So hand crafted code would look like this:
addressEntity.Id = AddressModel.Id;
addressEntity.Street = AddressModel.Street
addressEntity.StateReference.EntityKey = new EntityKey("MyDB.States", "Id", AddressModel.State.Id);
Obviously, when automapper tries to assign an Address.State.Id to the equivalent in EF, an exception is thrown.
I came across the following blog that explains the nature of my problem and solves it by way to Foreign Key Properties supported in EF4.0
http://blogs.msdn.com/efdesign/archive/2009/03/16/foreign-keys-in-the-entity-framework.aspx
精彩评论