如何从System.Data.Entities.DynamicProxies获取EF POCO [英] How to get EF POCOs from System.Data.Entities.DynamicProxies

查看:100
本文介绍了如何从System.Data.Entities.DynamicProxies获取EF POCO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是与此相同



然而,我真的没有看到一个解决方案。说我有一个简单的模型与两个POCO,国家和国家。

  public class Country 
{
public string Code {get;组; }
public string Name {get;组; }
}
public class State
{
public string Code {get;组; }
public string Name {get;组; }

public virtual国家国家{get;组; }
}

当我使用存储库.GetStateByCode(myCode)时,它检索一个动态代理对象。我想通过WCF服务通过线路发送给我的客户端。动态代理不是知道类型,所以它失败。



这是我的替代品。我可以在上下文中将ProxyCreationEnabled设置为false,然后我的.GetStateByCode(myCode)给我一个非常好的POCO。然而,POCO到国家的导航属性是NULL(不是很大)。我应该新建状态POCO并手动填充并从存储库返回的动态代理返回该状态?我应该尝试使用AutoMapper将动态代理对象映射到POCO吗?有没有什么我完全错过这里?

解决方案

我认为Ladislav Mrnka的答案是清楚的。警告仍然适用。即使以下这个想法。成为捡起来的东西他只是没有包括,如果你想继续如何轻松地从Object a获取数据到对象B.这是正确的问题。



示例解决方案



参见nuget包 ValueInjecter (不是唯一可以做到这一点但是非常容易使用的工具)
它允许轻松复制一个反对另一个,特别是具有相同的属性和类型。
(记住懒惰的加载/导航含义)。



所以,vanilla选项是:

  var PocoObject = new Poco(); 
PocoObject.InjectFrom(DynamicProxy); //将DynamicProxy的内容复制到PocoObject

但检查默认行为,并考虑自定义规则

  var PocoObject = new Poco(); 
PocoObject.InjectFrom< CopyRule>(DynamicProxy);

public class CopyRule:ConventionInjection
{
protected override bool Match(ConventionInfo c)
{
bool usePropertry; //返回如果属性包含在注入过程中
usePropertry = c.SourceProp.Name ==Id; //只是一个例子
//或
// usePropertry = c.SourceProp.Type ... ==???
return usePropertry;
}
}


My question is the same as this one

However, I don't really see a solution there. Lets say I have a simple model with two POCOs, Country and State.

public class Country
{
    public string Code { get; set; }
    public string Name { get; set; }
}
public class State 
{
    public string Code { get; set; }
    public string Name { get; set; }

    public virtual Country Country { get; set; }
}

When I use the repository to .GetStateByCode(myCode), it retrieves a dynamic proxy object. I want to send that over the wire using a WCF service to my client. The dynamic proxy is not a know type so it fails.

Here are my alternatives. I can set ProxyCreationEnabled to false on the context and then my .GetStateByCode(myCode) gives me a POCO which is great. However, the navigation property in the POCO to Country is then NULL (not great).

Should I new up a state POCO and manually populate and return that from the dynamic proxy that is returned from the repository? Should I try to use AutoMapper to map the dynamic proxy objects to POCOs? Is there something I'm totally missing here?

解决方案

I think the answer from Ladislav Mrnka is clear. The Warnings Still apply. Even with this idea below. Becareful what gets picked Up. He just didnt include , if you want to proceed how to easily get data from Object a to object B. That is question at hand really.

Sample solution

See nuget package ValueInjecter (not the only tool that can do this... but very easy to use) it allows easy copying of One object to another especially with the same properties and types. ( remember the lazy loading / navigation implications).

So vanilla option is :

 var PocoObject = new Poco();
 PocoObject.InjectFrom(DynamicProxy); // copy contents of DynamicProxy to PocoObject

but check the default behaviour and consider a custom rule

     var PocoObject = new Poco();
     PocoObject.InjectFrom<CopyRule>(DynamicProxy);

 public class CopyRule : ConventionInjection
    {
        protected override bool Match(ConventionInfo c)
        {
            bool usePropertry;   // return if the property it be included in inject process
            usePropertry = c.SourceProp.Name == "Id";  // just an example
            //or
            // usePropertry = c.SourceProp.Type... == "???"
            return usePropertry;
        }
    }

这篇关于如何从System.Data.Entities.DynamicProxies获取EF POCO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆