简体   繁体   English

在LINQPad中使用数据上下文调用静态方法

[英]Calling Static Methods with Data Contexts in LINQPad

I am using LINQPad to develop a new data access method. 我正在使用LINQPad开发一种新的数据访问方法。 In the method, which performs a LINQ-to-SQL query against a table in MySchemaContext, I am using a parameter for which I am loading a default value from the same DataContext object using a static method. 在对MySchemaContext中的表执行LINQ-to-SQL查询的方法中,我使用的参数是使用静态方法从同一DataContext对象中加载默认值的参数。

When I call this static function in LINQPad (using the "C# Program" setting) - one that works fine when called from with an application, and that is public - I get a null reference exception on the line that assigns the return value of the static function to a variable. 当我在LINQPad中调用此静态函数(使用“ C#程序”设置)时,该函数在从应用程序中调用时正常工作,并且是公共的,因此在分配该函数的返回值的行上出现空引用异常。将静态函数转换为变量。 If I change the logic of the function, it works if I do not reference MySchemaContext (for instance, if I assemble the List by hand and return that), but throws the exception if I do reference MySchemaContext. 如果更改函数的逻辑,则在不引用MySchemaContext的情况下(例如,如果我手工组装List并返回该列表),它将起作用,但在引用MySchemaContext的情况下会引发异常。

My guess is that this has something to do with the way LINQPad is connecting to my database - that the DataContext is not valid when the library function is called from LINQPad, and that the null reference exception is being thrown by GetTable<MyClassTable> in the library. 我的猜测是这与LINQPad连接到我的数据库的方式有关-当从LINQPad调用库函数时,DataContext无效,并且GetTable<MyClassTable>抛出null引用异常。图书馆。 Is this a limitation of LINQPad, or is there something obvious I'm missing? 这是LINQPad的限制吗?或者我明显缺少某些东西吗?

Here is a sample of the sort of function I'm having a problem with. 这是我遇到问题的函数示例。 The first part is the code in my library (which I am calling from the LINQPad query): 第一部分是我的库中的代码(我从LINQPad查询中调用):

namespace MyLibrary
{
 public class MyClass : MyClassTable
 {
   //There are reasons not to just add stuff to the partial class MyClassTable 
   //that aren't shown in this generic example

  public int MyClassTemplateID {get; set;}
  public int MyInt {get; set;}
  public string MyString {get; set;}

  public MyClass() {}
  public MyClass(int myClassTemplateID, int myInt, string myString) 
  {
   MyClassTemplateID = myClassTemplateID;
   MyInt = myInt;
   MyString = myString;
  }
  public static List<MyClass> GetMyClassesTemplate(int myClassTemplateID)
  {
   //MySchemaContext is a LINQ-to-SQL DataContext object, i.e., inherits from System.Data.Linq.DataContext 
   using (MySchemaContext myContext = new MySchemaContext())
   {
    return (from mct in myContext.GetTable<MyClassTable>().AsQueryable() 
     where mct.MyClassTemplateID == myClassTemplateID 
     select new MyClass
     {
      MyClassTemplateID = mct.MyClassTemplateID,
      MyInt = mct.MyInt,
      MyString = mct.MyString
     }).ToList<MyClass>();
   }
  }
 }
}

The library function works well in my application. 库函数在我的应用程序中运行良好。 This second part is in my LINQPad query, trying to call this same function: 第二部分在我的LINQPad查询中,尝试调用此相同的函数:

/*
    Call from LINQPad
    MyLibrary.dll is in "Additional References," and MyLibrary is in "Additional Namespace Imports."
*/

void Main()
{
 List<MyClass> myClassList = MyClass.GetMyClassesTemplate(1);
 //get error "NullReferenceException: Object reference not set to an instance of an object.
 //However, this exact line of code works fine in VisualStudio.
 //Have correct connection string for MySchemaContext in LINQPad.exe.config and lprun.exe.config
}

I suspect your issue is the use of the separate namespace. 我怀疑您的问题是使用单独的名称空间。 I was able to access a static method in LINQPad using the following code. 我可以使用以下代码在LINQPad中访问静态方法。 Nptice in particular the mismatch of the opening and closing brackets. 特别要注意的是,开括号和闭括号的不匹配。

    void Main()
    {
        tName.test.DoIt(); // Fully qualify the static method name
    }

} // Close the generated context's class

namespace tName
{
    public class test
    {
    // Define other methods and classes here
        public static void DoIt()
        {
            Console.WriteLine("Done");
        }
    }
// } Don't close the namespace. Let LinqPad do it.

With LinqPad, you are essentially injecting the code you type into a generated context. 使用LinqPad,您实际上是将键入的代码注入到生成的上下文中。 As a result, you need to fake it out thinking that the context's class is closed before injecting the new namespace/class. 结果,您需要在注入新的名称空间/类之前以为上下文的类已关闭来伪造它。 If you didn't use the namespace, you would be able to create a new class, but that class would be a child of the hosting class, not directly under the namespace in question. 如果不使用命名空间,则可以创建一个新类,但是该类将是托管类的子类,而不是直接在所涉及的命名空间下。 I discussed this further in this blog post . 我在这篇博客文章中对此进行了进一步讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM