Entity framework 4 Covnert实体图到数据集

Entity framework 4 Covnert实体图到数据集,entity-framework-4,dataset,entity,poco,Entity Framework 4,Dataset,Entity,Poco,我正在使用EF4并创建了POCO对象。POCO对象的问题是所有实体图都是ICollection…因此无法序列化。我已经解决了通过WCF传递POCO对象的问题 问题在于将实体图传递给存储过程……因此,我采取的方法是将实体图转换为数据集,将数据集转换为xml,然后将其传递给存储过程……这是在存储过程中获得干净xml的唯一方法 我正在尝试创建一个通用的助手方法来将实体图转换为数据集 public static DataSet ObjectToDataSet<T>(IEnumerable&

我正在使用EF4并创建了POCO对象。POCO对象的问题是所有实体图都是ICollection…因此无法序列化。我已经解决了通过WCF传递POCO对象的问题

问题在于将实体图传递给存储过程……因此,我采取的方法是将实体图转换为数据集,将数据集转换为xml,然后将其传递给存储过程……这是在存储过程中获得干净xml的唯一方法

我正在尝试创建一个通用的助手方法来将实体图转换为数据集

 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)varlist.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);
            }

        }
公共静态数据集ObjectToDataSet(IEnumerable varList)
{
数据集ds=新数据集();
if(varList==null)
返回ds;
ObjectToDataTable(变量列表,ds);
返回ds;
}
公共静态void ObjectToDataTable(IEnumerable变量列表,数据集ds)
{
if(varlist==null)
回来
//列名
PropertyInfo[]oProps=null;
字符串tableName=typeof(T).Name;
bool tableExits=ds.Tables.Contains(tableName);
DataTable dt=新的DataTable();
//检查数据集中是否存在表
如果(!tableExits)
{
dt=新的数据表(typeof(T).Name);
ds.Tables.Add(dt);
oProps=((Type)varlist.First().GetType()).GetProperties();
foreach(oProps中的属性信息pi)
{
类型colType=pi.PropertyType;
如果(!colType.IsGenericType)
{
if(colType!=typeof(EntityKey))
Add(新数据列(pi.Name,colType));
}
其他的
if((colType.IsGenericType)&&(colType.GetGenericTypeDefinition()==typeof(null)))
Add(新数据列(pi.Name,colType.GetGenericArguments()[0]);
其他的
if(pi.PropertyType.GetGenericTypeDefinition()==typeof(ICollection))
{
//无所事事
}
}
}
如果(表格出口)
dt=ds.Tables[tableName];
foreach(varlist中的T rec)
{
DataRow dr=dt.NewRow();
foreach(oProps中的属性信息pi)
{
if(pi.PropertyType.Namespace!=typeof(System.Collections.Generic.ICollection.Namespace)
dr[pi.Name]=pi.GetValue(rec,null)=null?DBNull.Value:pi.GetValue(rec,null);
其他的
{
Type Type=pi.PropertyType.GetGenericArguments()[0];
//这将是一种重复的方法
//如何获取类型T和传递给方法ObjectToDataTable的值
//这里需要帮助吗
ObjectToDataTable<
}
}
dt.Rows.Add(dr);
}
}
看一看。
它处理POCO序列化,希望这是您需要的。
似乎解决了你最初的问题