我有一个POCO,我可以从一个的DbContext代理? [英] I have a POCO, can I get a proxy from the DbContext?

查看:132
本文介绍了我有一个POCO,我可以从一个的DbContext代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型,我从一个POST请求得到。由于我的视图定义其POCO类型,从提交的数据创建的对象也是一个POCO。作为一个POCO,它不具有各种虚拟属性覆盖。因此,这些虚拟财产返回null。这,反过来,意味着我必须基于外键,通过它的属性进行导航(如果我想做的任何事情更复杂的不仅仅是保存)进行单独的查询。

I have a model that I get from a POST request. Since my view defines its POCO type, the object created from the submitted data is also a POCO. Being a POCO, it doesn't have various virtual properties overridden. Therefore, those virtual properties return null. This, in turn, means that I have to make separate queries based on the foreign keys to navigate through its properties (if I want to do anything more complex than just saving it).

我,给我的模型的POCO,得到了所有被覆盖的功能代理

(我认为这是 db.Entry()。实体是,但它仍然返回我的POCO对象,而不是代理。我检查通过在断点停顿鼠标悬停对象的运行时类型。)

(I had assumed that this is what db.Entry().Entity was for, but it still returns me the POCO object, not the proxy. I'm inspecting the runtime type of the object by mouse-over during breakpoint pauses.)

推荐答案

沿着这code的东西线会做你的需要。我用 automapper ,向在实体传递给代理的版本复制值。

Something along the lines of this code will do what you need. I've used automapper to copy values from the passed in entity to the proxied version.

在code检查是否在实体传递是一个代理与否,并相应地处理它。

The code checks whether the passed in entity is a proxy or not and handles it accordingly.

public class Repository<T> where T : class
{
    private readonly Context context;
    private bool mapCreated = false;
    public Repository(Context context)
    {
        this.context = context;
    }

    protected virtual T InsertOrUpdate(T e, int id)
    {
        T instance = context.Set<T>().Create();
        if (e.GetType().Equals(instance.GetType()))
            instance = e;
        else
        {
            if (!mapCreated)
            {
                Mapper.CreateMap(e.GetType(), instance.GetType());
                mapCreated = true;
            }
            instance = Mapper.Map(e, instance);
        }

        if (id == default(int))
            context.Set<T>().Add(instance);
        else
            context.Entry<T>(instance).State = EntityState.Modified;

        return instance;
    }
}


更新:由@Colin在评论中所述,这并不需要版本 automapper


UPDATE version as described by @Colin in the comments that does not need automapper

public class Repository<T> where T : class
{
    private readonly Context context;
    public Repository(Context context)
    {
        this.context = context;
    }

    protected virtual T InsertOrUpdate(T e, int id)
    {
        T instance = context.Set<T>().Create();
        if (e.GetType().Equals(instance.GetType()))
        {
            instance = e;
        }
        else
        {
            DbEntityEntry<T> entry = context.Entry(instance);
            entry.CurrentValues.SetValues(e);
        }

        context.Entry<T>(instance).State =
            id == default(int)
                ? EntityState.Added
                : EntityState.Modified;

        return instance;
    }
}

这篇关于我有一个POCO,我可以从一个的DbContext代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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