将POCO映射到实体框架中的实体 [英] Mapping POCO to Entity in Entity Framework
问题描述
由于我们有一个n层应用程序,我们正在映射POCO到实体不断,实体不会被抛到上层,我用Automapper进行映射,手动也是。
我们遇到的大问题是从POCO映射到
如果我添加了一个与现有实体有关系的新实体(例如为现有客户添加新帐户)我有现有的实体POCO,需要从数据库获取相关实体(例如客户端),这只是一个非常慢的。
我的问题是:
如何创建一个实体,正确附加到上下文以及所有这些,从POCO对象,没有调用数据库?
设置 EntityKey
而不是加载相关的obj从数据库(注意,有一个更好的方法来做这个在EF 4;这是为EF 1):
var account = new EntityAccount
{
Name = pocoAccount.Name ,
//等
};
//现在而不是做account.Client = Context.Clients.Where(...
account.ClientReference.EntityKey =
new EntityKey(MyEntities.Clients,Id pocoAccount.Client.Id);
根本没有DB访问。
I use EF for Visual Studio 2008, so I don't have POCO integration with EF.
Since we have a n-tier application, we're mapping POCO to entities constantly, entities aren't thrown to the upper layers, I do mapping with Automapper and manually also.
The big problem we have is when mapping from POCO to entities.
If I'm adding a new entity that has a relationship with existing entities (e.g. adding a new Account for an existing Client), I have the existing entity POCO, and it's needed to get the related entities (e.g. the Client) from the database, this is just something very slow.
And there is my question:
How can I create an entity, properly attached to the context and all that, from a POCO object, without making any call to the database?
Set the EntityKey
instead of loading the related object from the DB (note there's a better way to do this in EF 4; this is for EF 1):
var account = new EntityAccount
{
Name = pocoAccount.Name,
// etc.
};
// now instead of doing account.Client = Context.Clients.Where(...
account.ClientReference.EntityKey =
new EntityKey("MyEntities.Clients", "Id", pocoAccount.Client.Id);
This does no DB access at all.
这篇关于将POCO映射到实体框架中的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!