C# 动态上下文数据集中的循环以及对象和属性

C# 动态上下文数据集中的循环以及对象和属性,c#,entity-framework,dynamic,reflection,objectcontext,C#,Entity Framework,Dynamic,Reflection,Objectcontext,我有一个上下文,我想在上下文中保存每个数据集中的所有数据,我不想基于每个类类型编写代码。所以我想要保存它们的类型列表,我想要在context.dataset中使用loop,然后在属性上使用loop 你能帮我吗 public static void saveAllFiles(Context context) { var objectTypes = new List<Type>(); objectTypes.Add(typeo

我有一个上下文,我想在上下文中保存每个数据集中的所有数据,我不想基于每个类类型编写代码。所以我想要保存它们的类型列表,我想要在context.dataset中使用loop,然后在属性上使用loop

你能帮我吗

     public static void saveAllFiles(Context context)
      {
            var objectTypes = new List<Type>();
        objectTypes.Add(typeof(Language));
        objectTypes.Add(typeof(Employee));

    foreach(Type objectType in objectTypes)
        {
            //var properties = objectType.GetProperties();
            var properties = objectType.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            var dataSetObjects = context.Set<objectType>().ToList();

            foreach(var dataSetObject in dataSetObjects)
            {

                 foreach( var  property in properties)
                 {
                     var value = property.GetValue(dataSetObject);
                     var name= property.Name;
                 }

            }
        }
}

我不确定您的上下文所指的是什么类,但以下是一些代码,让您了解如何获取属性名称和值:

public static void saveAllFiles(Context context)
{
    var objectTypes = new List<Type> {typeof (int), typeof (string)}; 

    foreach(Type objectType in objectTypes)
    {
        var properties = objectType.GetProperties(BindingFlags.Instance | BindingFlags.Public);
        var dataSetObjects = context.Set(objectType);

        foreach(var dataSetObject in dataSetObjects)
        {
            foreach( var property in properties )
            {
                var value = property.GetValue(dataSetObject);
                var name = property.Name;
            }                   
        }
    }
}

我现在不能使用这一行:context.Wherex=>x.GetType==objectType;例如,mycontext有context.employees和context.languages,我希望有一个对象类型列表,以便在该列表中循环使用listvar objectTypes=new list;objectTypes.AddtypeofLanguage;objectTypes.AddtypeofEmployee;我现在有问题:context.Set.ToList和->property.getValueDataSetObject什么类型是上下文?我不知道那是什么类型的,所以很难帮上忙。您遇到了什么问题?上下文是由我自己继承定义的:public-class-context:DbContext,IContext