2012-01-27 75 views
1

考慮我已經構建了DAL.dll,它是一個包含實體框架edmx的類庫。在Designer.cs,進口下列存儲過程定義:使用反射從實體數據模型獲取存儲過程名稱。

<Function Name="Login_User" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo"> 
     <Parameter Name="Login_Name" Type="nvarchar" Mode="In" /> 
     <Parameter Name="Password" Type="nvarchar" Mode="In" /> 
     <Parameter Name="SP_Return_Code" Type="int" Mode="InOut" /> 
    </Function> 

下面我已經使用反射來找到TYPE1作爲ObjectContext的類型。如何通過反映type1來發現Login_User存儲過程?

private static void ReflectionTest() 
    { 
     var asm = Assembly.LoadFrom(@"C:\DAL.dll"); 

     // list stored procedure calls 
     foreach (var type in asm.GetTypes()) 
     { 
      if (type.BaseType == typeof(ObjectContext)) 
      { 
       foreach (var type1 in type.GetMethods()) 
       { 
        // how do I reflect against type1 for its stored procedure names? 
       } 
      } 
     } 
    } 
+0

? – 2012-01-28 10:48:00

回答

2
  1. 首先,你需要導入您的存儲過程作爲Function在你的實體模型。請參閱以下鏈接以獲取詳細信息:http://ashishrocks.wordpress.com/2010/09/05/entity-framework-using-select-stored-procedures-for-entities-having-same-column-names/

  2. 當您正在執行此操作時,請確保您爲Function Import Name使用某種命名約定。例如,前綴SP_用於所有存儲過程函數導入。

  3. 將您的SP添加爲實體模型中的函數後,您應該能夠在您的Model.Designer.cs中看到它們。編譯更新的DAL。

現在,你可以得到這樣的存儲過程:你爲什麼要使用反射此

Assembly assembly = Assembly.LoadFrom(@"C:\DAL.dll"); 

foreach (MethodInfo methodInfo in from type in assembly.GetTypes() 
            where type.BaseType == typeof (ObjectContext) 
            from methodInfo in type.GetMethods() 
            where methodInfo.Name.StartsWith("SP_") 
            select methodInfo) 
{ 
    Console.WriteLine(methodInfo.Name); 
} 

Console.ReadLine(); 
相關問題