Domanda

I have a database with several hundred tables imported from a third source. Using entity framework, these tables become ObjectSets labeled E_1, E_2, E_3... All of the tables have a common set of columns and can be queried with one function.

I have the following code:

namespace Foo{
    public static class Data{

       public static MyEntities dataContext = new MyEntities();

       public static void getData<T>(string entityName) where T : class
       {
          string queryString = "SELECT result FROM ";
          queryString += Data.dataContext.DefaultContainerName + "." + entityName;

          ObjectQuery<T> myQuery = Data.dataContext.CreateQuery<T>(queryString, null);

        // do stuff
       }

       public static void test(){
         string entityName = "E_1";
         Data.getData<E_1>(entityName);
       }
}

In test(), I pass the string "E_1" to the function getData(), and I also insert type class Foo.E_1 into getData<> as the generic type parameter.

In reality, E_1 isn't known until run time. At run time, I create the entityName string, but how can I then convert this string into the generic parameter type needed for getData<>?

Thanks.

È stato utile?

Soluzione

All of the tables have a common set of columns

So you can make one class (e.g. MyImportType) to capture the results of your query, and use ExecuteStoreQuery in stead of ObjectQuery

var data = Data.dataContext.ExecuteStoreQuery<MyImportType>(queryString);

And getData does not need a generic type parameter.

As aside: I agree with TomTom that the db design is very bad. If you can, at least merge the tables with identical columns into one table.

Altri suggerimenti

In reality, E_1 isn't known until run time

Anjd in reality that is:

  • VERY bad database design and
  • SOmething not supported by Entity Framework.

Simple like that.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top