检索导航属性更改上下文 [英] Retrieving Navigation Properties changes context

查看:48
本文介绍了检索导航属性更改上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有以下代码的存储库类,用于更新已更改的实体。
但是我遇到了错误

I have a repository class with the following code to update entities that have changed. However I get an error

"The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects."

但是由于上下文是我存储库的属性,因此我看不到上下文已更改。

However I cant see that the context has changed since it is a property of my repository.

在我拥有的存储库中

protected  void InnerUpdate(params T[] items)
    {
        DbSet<T> dbSet = ((DbContext)this.context).Set<T>();
        foreach (T item in items)
        {
                    object id1 = item.GetProperty("Id");
                    T originalEntity = dbSet.Find(id1);
                    ((DbContext)this.context).Entry(originalEntity).CurrentValues.SetValues(item);

                    var navProps = GetNavigationProperties(originalEntity);
                    foreach (var navProp in navProps)
                    {
                        //Set originalEntity prop value to modifiedEntity value
                        navProp.SetValue(originalEntity, navProp.GetValue(item));

                    }
            }
        }

        this.context.SaveChanges();  // Error occurs here
    }


    public List<PropertyInfo> GetNavigationProperties(T entity )
    {
        var t = entity.GetType();
        ObjectContext objectContex = ((IObjectContextAdapter)((DbContext)this.context)).ObjectContext;

        var elementType = objectContex.CreateObjectSet<T>().EntitySet.ElementType;


        var properties = new List<PropertyInfo>();
        var entityType = entity.GetType();
        foreach (var navigationProperty in elementType.NavigationProperties)
        {
            properties.Add(entityType.GetProperty(navigationProperty.Name));
        }
        return properties;
    }

我想知道问题是否出在调用CreateObjectSet吗?有其他方法可以做到吗?
我正在使用EF6.1

I wonder if the problem is due to calling CreateObjectSet ? Is there a different way to do this? I am using EF6.1

推荐答案

我按如下所示更改了代码

I changed the code as follows and got it working

                    var navProps = GetNavigationProperties(originalEntity);
                    foreach (var navProp in navProps)
                    {
                        //Set originalEntity prop value to modifiedEntity value
                        var newval = (LoggedEntity) navProp.GetValue(item);
                        object entity = null;
                        if (newval != null)
                        {
                            var tp = navProp.PropertyType;
                            var entities = ((DbContext)this.context).Set(tp);
                            entity = entities.Find(newval.Id);
                        }
                        navProp.SetValue(originalEntity, entity);
                    }

这篇关于检索导航属性更改上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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