Get Stored Procedure from Data Context : Linq to SQl

前端 未结 3 1154
旧时难觅i
旧时难觅i 2020-12-21 06:34

I have a stored procedure named ParseXML in SQL Server. I have a repository pattern using LINQ to SQL. I need to call the stored procedure from within the repository layer.

3条回答
  •  离开以前
    2020-12-21 07:00

    You can do something like this, calling the method using reflection:

    var inputXML = GetXML(); 
    
    var method = Context.GetType().GetMethod("ParseXML");
    
    if(method == null) throw new InvalidOperationException("Defined DataContext does not have method ParseXML");
    
    var result = method.Invoke(Context, new object[]{ inputXML });
    

    If you are using c# 4.0, you can do:

    var inputXML = GetXML(); 
    
    dynamic dynamicContext = Context;
    
    var result = (ISingleResult)dynamicContext.ParseXML(inputXML);
    

提交回复
热议问题