public static void SetReference(
            this ObjectContext oc,
            object target,
            string propertyName,
            object propertyValue)
        {
            EntityType entityType = oc.MetadataWorkspace.GetItem <EntityType>(
                target.GetType().FullName,
                DataSpace.CSpace);
            NavigationProperty navProperty = entityType.NavigationProperties[propertyName];
            ObjectStateEntry   entry       = oc.ObjectStateManager.GetObjectStateEntry(target);
            EntityReference    relatedEnd  = (EntityReference)entry.RelationshipManager
                                             .GetRelatedEnd(navProperty.RelationshipType.Name, navProperty.ToEndMember.Name);

            if (entry.State != EntityState.Added && !relatedEnd.IsLoaded)
            {
                relatedEnd.Load();
            }

            if (propertyValue == null)
            {
                relatedEnd.EntityKey = null;
            }
            else
            {
                EqualityComparer <EntityKey> keycomp = EqualityComparer <EntityKey> .Default;
                EntityKey relatedKey = oc.ObjectStateManager.GetObjectStateEntry(target).EntityKey;
                if (!keycomp.Equals(relatedEnd.EntityKey, relatedKey))
                {
                    ReflectionHelper.SetProperty(relatedEnd, "Value", propertyValue);
                }
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Ensures that entity referenced by <paramref name="entityReference"/> is loaded.
 /// </summary>
 public static EntityReference <TEntity> GetEnsureLoadedReference <TEntity>(this EntityReference <TEntity> entityReference) where TEntity : EntityObject
 {
     if (!entityReference.IsLoaded)
     {
         entityReference.Load();
     }
     return(entityReference);
 }
Exemplo n.º 3
0
        public static TEntity GetValueAutoLoaded <TEntity>(this EntityReference <TEntity> reference)
            where TEntity : class, IEntityWithRelationships
        {
            if (!reference.IsLoaded)
            {
                reference.Load();
            }

            return(reference.Value);
        }
Exemplo n.º 4
0
        public static T LoadAs <R, T>(this EntityReference <R> reference)
            where T : class, new()
            where R : EntityObject
        {
            if (reference == null)
            {
                return(null);
            }

            reference.Load();
            return(reference.Value.CopyProperties <T>());
        }
Exemplo n.º 5
0
        public static T GetKey <T>(this EntityObject entity, string foreignName, string keyName)
        {
            EntityReference er = entity.GetType().GetProperty(foreignName + "Reference").GetValue(entity, null) as EntityReference;
            EntityObject    eo = entity.GetType().GetProperty(foreignName).GetValue(entity, null) as EntityObject;

            if (er.EntityKey == null)
            {
                er.Load();
            }
            else if (!er.IsLoaded)
            {
                return((T)er.EntityKey.EntityKeyValues[0].Value);
            }

            return(eo == null ? default(T) : (T)eo.GetType().GetProperty(keyName).GetValue(eo, null));
        }
Exemplo n.º 6
0
 /// <summary>
 /// Loads the entity reference or its value if it hasn't already been loaded.
 /// </summary>
 /// <typeparam name="T">Type of entity reference</typeparam>
 /// <param name="entitySource">The source entity which has the entity reference relationship (added, modified or unchanged only)</param>
 public void EnsureLoaded <T>(EntityReference <T> entityReference, EntityObject entitySource) where T : class, IEntityWithRelationships
 {
     if (entitySource != null && entityReference != null && !entityReference.IsLoaded && entityReference.EntityKey != null)
     {
         if (entitySource.EntityState == System.Data.EntityState.Added) // add the value directly as load will throw
         {
             if (entityReference.Value == null)
             {
                 entityReference.Value = LoadByKey <T>(entityReference.EntityKey);
             }
         }
         else if (entitySource.EntityState == System.Data.EntityState.Modified || entitySource.EntityState == System.Data.EntityState.Unchanged)
         {
             entityReference.Load();
         }
     }
 }
Exemplo n.º 7
0
        static void Listing21_18()
        {
            Console.WriteLine("{0} : Begin", new StackTrace(0, true).GetFrame(0).GetMethod().Name);

            NorthwindEntities context = new NorthwindEntities();

            // disable lazy loading
            context.ContextOptions.LazyLoadingEnabled = false;

            // query for the order
            Order ord = (from o in context.Orders
                         where o.CustomerID == "LAZYK"
                         select o).First();

            // get the entity reference
            EntityReference <Customer> customerRef = ord.CustomerReference;

            // explicitly load the order
            customerRef.Load();

            Console.WriteLine("Customer name: {0}", customerRef.Value.CompanyName);

            Console.WriteLine("{0} : End", new StackTrace(0, true).GetFrame(0).GetMethod().Name);
        }
Exemplo n.º 8
0
        /// <summary>
        /// 保存数据变化到引擎服务
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="oldObject">修改前的实体</param>
        /// <param name="newObject">修改后的数据</param>
        /// <returns>保存结果</returns>
        public static string SaveTriggerData <T>(T oldObject, T newObject) where T : class
        {
            Type etype = oldObject.GetType();

            PropertyInfo[] infos = etype.GetProperties();

            EntityObject oldEnt = oldObject as EntityObject;
            EntityObject newEnt = newObject as EntityObject;

            XmlDocument    xd = new XmlDocument(); //表示XML文档
            XmlDeclaration xde;                    //表示 XML 声明节点:<?xml version='1.0'...?>

            xde            = xd.CreateXmlDeclaration("1.0", null, null);
            xde.Encoding   = "gb2312";
            xde.Standalone = "yes";
            xd.AppendChild(xde);                       //<?xml version="1.0" encoding="UTF-8" standalone="yes"?>生成结束

            XmlElement xe = xd.CreateElement("table"); //创建一个table根元素

            xd.AppendChild(xe);                        //table根元素创建完成

            //查找<table>
            XmlNode table = xd.SelectSingleNode("table");

            //在<table>之下创建元素<ApplicationSystem>
            XmlElement ApplicationSystem = xd.CreateElement("ApplicationSystem");

            //人事系统
            ApplicationSystem.AppendChild(xd.CreateTextNode("0"));
            table.AppendChild(ApplicationSystem);


            //在<table>之下创建元素<CompanyCode>
            XmlElement   CompanyCode = xd.CreateElement("CompanyCode");
            PropertyInfo corpProp    = infos.SingleOrDefault(p => p.Name == "UPDATEDEPARTMENTID");

            if (corpProp != null)
            {
                object corp = corpProp.GetValue(newObject, null);
                CompanyCode.AppendChild(xd.CreateTextNode(corp == null ? "" : corp.ToString()));
                table.AppendChild(CompanyCode);
            }

            //在<table>之下创建元素<OperationUser>
            XmlElement OperationUser = xd.CreateElement("OperationUser");
            //获取修改人的信息
            PropertyInfo userProp = infos.SingleOrDefault(p => p.Name == "UPDATEUSERID");

            if (userProp != null)
            {
                object user = userProp.GetValue(newObject, null);
                OperationUser.AppendChild(xd.CreateTextNode(user == null ? "" : user.ToString()));
                table.AppendChild(CompanyCode);
            }

            //在<table>之下创建元素<TableName>
            XmlElement TableName = xd.CreateElement("TableName");

            TableName.AppendChild(xd.CreateTextNode(oldObject.GetType().Name));
            table.AppendChild(TableName);


            if (oldEnt == null || newEnt == null)
            {
                return("");
            }

            //添加<TableKey>
            XmlElement TableKey = xd.CreateElement("TableKey");

            table.AppendChild(TableKey);

            if (oldEnt != null && oldEnt.EntityKey != null && oldEnt.EntityKey.EntityKeyValues != null && oldEnt.EntityKey.EntityKeyValues.Count() > 0)
            {
                foreach (var key in oldEnt.EntityKey.EntityKeyValues)
                {
                    //TableKeyName
                    XmlElement TableKeyName = xd.CreateElement("TableKeyName");
                    TableKeyName.AppendChild(xd.CreateTextNode(key.Key));
                    TableKey.AppendChild(TableKeyName);

                    XmlElement TableKeyValue = xd.CreateElement("TableKeyValue");
                    TableKeyValue.AppendChild(xd.CreateTextNode((key.Value == null) ? "" : key.Value.ToString()));
                    TableKey.AppendChild(TableKeyValue);
                }
            }

            //添加<FieldString>
            foreach (PropertyInfo prop in infos)
            {
                if (prop.PropertyType.BaseType == typeof(EntityReference) ||
                    prop.PropertyType.BaseType == typeof(RelatedEnd) ||
                    prop.PropertyType == typeof(System.Data.EntityState) ||
                    prop.PropertyType == typeof(System.Data.EntityKey)
                    )
                {
                    continue;
                }


                //关键字段跳过
                if (oldEnt != null && oldEnt.EntityKey != null && oldEnt.EntityKey.EntityKeyValues != null && oldEnt.EntityKey.EntityKeyValues.Count() > 0)
                {
                    bool isKeyField = false;
                    foreach (var key in oldEnt.EntityKey.EntityKeyValues)
                    {
                        if (key.Key == prop.Name)
                        {
                            isKeyField = true;
                            break;
                        }
                    }
                    if (isKeyField)
                    {
                        continue;
                    }
                }


                //生成外键的
                if (prop.PropertyType.BaseType == typeof(System.Data.Objects.DataClasses.EntityObject))
                {
                    PropertyInfo refProp = infos.SingleOrDefault(p => p.Name == (prop.Name + "Reference"));

                    EntityReference reference = refProp.GetValue(oldEnt, null) as EntityReference;

                    if (!reference.IsLoaded)
                    {
                        reference.Load();
                    }

                    EntityObject oldEntRef = prop.GetValue(oldEnt, null) as EntityObject;
                    EntityObject newEntRef = prop.GetValue(newEnt, null) as EntityObject;

                    foreach (var key in oldEntRef.EntityKey.EntityKeyValues)
                    {
                        //在<table>之下创建元素<FieldString>
                        XmlElement FieldString = xd.CreateElement("FieldString");
                        table.AppendChild(FieldString);

                        XmlElement FieldName = xd.CreateElement("FieldName");
                        FieldName.AppendChild(xd.CreateTextNode(key.Key));
                        FieldString.AppendChild(FieldName);

                        XmlElement Field_Old_Value = xd.CreateElement("Field_Old_Value");
                        Field_Old_Value.AppendChild(xd.CreateTextNode((key.Value == null) ? "" : key.Value.ToString()));
                        FieldString.AppendChild(Field_Old_Value);

                        XmlElement Field_New_Value = xd.CreateElement("Field_New_Value");

                        PropertyInfo[] refinfos        = prop.PropertyType.GetProperties();
                        PropertyInfo   refEntProp      = refinfos.SingleOrDefault(p => p.Name == key.Key);
                        object         refEntPropValue = refEntProp.GetValue(newEntRef, null);

                        Field_New_Value.AppendChild(xd.CreateTextNode((refEntPropValue == null) ? "" : refEntPropValue.ToString()));
                        FieldString.AppendChild(Field_New_Value);
                    }
                }
                else
                {
                    //prop.Name
                    //在<table>之下创建元素<FieldString>
                    XmlElement FieldString = xd.CreateElement("FieldString");
                    table.AppendChild(FieldString);

                    XmlElement FieldName = xd.CreateElement("FieldName");
                    FieldName.AppendChild(xd.CreateTextNode(prop.Name));
                    FieldString.AppendChild(FieldName);

                    XmlElement Field_Old_value = xd.CreateElement("Field_Old_Value");
                    object     oldvalue        = prop.GetValue(oldEnt, null);
                    Field_Old_value.AppendChild(xd.CreateTextNode((oldvalue == null) ? "" : oldvalue.ToString()));
                    FieldString.AppendChild(Field_Old_value);

                    XmlElement Field_New_value = xd.CreateElement("Field_New_Value");
                    object     newvalue        = prop.GetValue(newEnt, null);
                    Field_New_value.AppendChild(xd.CreateTextNode((newvalue == null) ? "" : newvalue.ToString()));
                    FieldString.AppendChild(Field_New_value);
                }
            }

            EngineWS.EngineWcfGlobalFunctionClient engClient = new EngineWS.EngineWcfGlobalFunctionClient();
            string rslt = string.Empty;// engClient.SaveTriggerData(xd.OuterXml);

            return(rslt);
        }