2012-01-27 62 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(); 
相关问题