简体   繁体   English

在实体框架中自动映射到EntityKeys

[英]Automapping to EntityKeys in Entity Framework

Does anyone have a technique to automap (using Automapper) references to child entities. 有没有人有一种技术可以自动映射(使用Automapper)对子实体的引用。 So say I have a ViewModel: 假设我有一个ViewModel:

class AddressModel
{
    int Id;
    string Street;
    StateModel State;
}

class StateModel
{
    int Id;
    string Name;
}

And I pass this into a repository to map to equivalent entities in Entity Framework. 然后,我将其传递到存储库中以映射到Entity Framework中的等效实体。 When Automapping, I want it to automap AddressModel.State.ID to the EntityKey of AddressEntity.StateReference. 自动映射时,我希望它自动将AddressModel.State.ID映射到AddressEntity.StateReference的EntityKey。 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. 显然,当自动映射器尝试将Address.State.Id分配给EF中的等效项时,将引发异常。

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 我遇到了以下博客,该博客解释了我的问题的性质,并通过EF4.0中支持的外键属性解决了该问题。

http://blogs.msdn.com/efdesign/archive/2009/03/16/foreign-keys-in-the-entity-framework.aspx http://blogs.msdn.com/efdesign/archive/2009/03/16/foreign-keys-in-the-entity-framework.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM