开发者

Covnert entity graph to dataset

开发者 https://www.devze.com 2023-02-07 15:20 出处:网络
I\'m using EF4 and have created POCO objects. The problem with POCO objects are all the entity graph are ICollection....therefore cannot be serialized. I have solved the issues of passing POCO object

I'm using EF4 and have created POCO objects. The problem with POCO objects are all the entity graph are ICollection....therefore cannot be serialized. I have solved the issues of passing POCO object via WCF.

The problem lies is passing the entity graph to stored procedure....therefore the approach i have take is to convert the entity graph to dataset, convert the dataset to xml and then pass it the stored procedure....this is the only way i could get a clean XML in my stored procedure.

I'm trying to create a generic helper method to convert entity graph to dataset.

 public static DataSet ObjectToDataSet<T>(IEnumerable<T> varList)
        {
            DataSet ds = new DataSet();

            if (varList == null)
                return ds;

            ObjectToDataTable(varList, ds);

            return ds;
        }

        public static void ObjectToDataTable<T>(IEnumerable<T> varlist, DataSet ds)
        {
            if (varlist == null)
                return;

            // column names 
            PropertyInfo[] oProps = null;

            string tableName = typeof(T).Name;

            bool tableExits = ds.Tables.Contains(tableName);

            DataTable dt = new DataTable();

            //check if table exits in the dataset
            if (!tableExits)
            {
                dt = new DataTable(typeof(T).Name);
                ds.Tables.Add(dt);

                oProps = ((Type)var开发者_如何学JAVAlist.First().GetType()).GetProperties();
                foreach (PropertyInfo pi in oProps)
                {
                    Type colType = pi.PropertyType;

                    if (!colType.IsGenericType)
                    {
                        if (colType != typeof(EntityKey))
                            dt.Columns.Add(new DataColumn(pi.Name, colType));
                    }
                    else
                        if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
                            dt.Columns.Add(new DataColumn(pi.Name, colType.GetGenericArguments()[0]));
                        else
                            if (pi.PropertyType.GetGenericTypeDefinition() == typeof(ICollection<>))
                            {
                                //do nothing
                            }

                }
            }

            if (tableExits)
                dt = ds.Tables[tableName];

            foreach (T rec in varlist)
            {
                DataRow dr = dt.NewRow();

                foreach (PropertyInfo pi in oProps)
                {
                    if (pi.PropertyType.Namespace != typeof(System.Collections.Generic.ICollection<>).Namespace)
                        dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue(rec, null);
                    else
                    {
                        Type type = pi.PropertyType.GetGenericArguments()[0];
                           //this would be a recuresive method
                        //how to get the Type T and the values to pass to method ObjectToDataTable

                        //need help here
                        ObjectToDataTable<

                    }
                }

                dt.Rows.Add(dr);
            }

        }


Take a look at this post.
It deals with the POCO serialization, hope this is what you need.
And this post seems to solve your initial problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号