如何在Visual Studio中使用LinqPad生成的上下文 [英] How can I use LinqPad's generated context in Visual Studio
问题描述
这实际上是该问题的后续内容:
This is a follow-on from this question really:
从LINQpad迁移到适当的Visual Studio项目?
..但是我无法使其正常工作.
..but I'm not able to get it to work properly.
该问题的答案建议将上下文程序集转储为dll,但是尽管我已经做到了,但是当我将其导入为引用时,我尚不清楚我将如何创建该上下文的实例,将其指向在数据库上并对其实际运行查询,如下所示:
An answer to that question suggestions dumping the context assembly out as a dll but although I have done that, when I import it as a reference, it's not exactly clear to me how I would create an instance of that context, point it at a database and actually run a query against it, something like the following:
var db = new ContextFromThatDLL(myconnectionstring);
var db = new ContextFromThatDLL(myconnectionstring);
var query = from db.MYTABLE中的a,其中a.ID == 1选择a;
var query = from a in db.MYTABLE where a.ID == 1 select a;
其他信息:
我正在使用LinqPad中的IQ驱动程序来连接到Oracle.
I am using the IQ driver in LinqPad to connect to Oracle.
我做已经拥有DevArt的许可证(IQ驱动程序使用该许可证),但是我知道IQ驱动程序从LINQ生成自己的SQL-我更喜欢它.另外,我在LinqPad中开发了对我的工作流程非常有用的查询,但是发现DevArt生成的SQL并不总是和IQ一样好.
I do have a license for DevArt already (which the IQ driver uses) but am aware that the IQ driver generates its own SQL from LINQ - and I prefer it. Plus, I develop queries in LinqPad which works great for my workflow but find that DevArt doesn't always generate SQL as good as IQ.
推荐答案
首先,按如下所示在LINQPad中提取类型化的数据上下文:
First, extract the typed data context in LINQPad as follows:
string dcPath = GetType().BaseType.Assembly.Location;
string targetFolder = @"c:\temp";
File.Copy (dcPath, Path.Combine (targetFolder, Path.GetFileName (dcPath)));
然后在Visual Studio中,引用类型化的数据上下文DLL,以及驱动程序文件夹中的以下DLL:
Then in Visual Studio, reference the typed data context DLL, along with the following DLLs from the driver folder:
- IQDriver.dll
- IQToolkit.dll
- IQToolkit.Data.dll
- IQToolkit.Data.(provider).dll
加上DevArt驱动程序.
plus the DevArt driver.
然后,您可以按以下方式实例化类型化的数据上下文(这说明了如何针对SQLite进行操作):
Then, you can instantiate the typed data context as follows (this illustrates how to do it for SQLite):
var dc = new LINQPad.User.TypedDataContext (IQToolkit.Data.DbEntityProvider.From
("IQToolkit.Data.Sqlite", @"Data Source=D:\SQLite.NET\nutshell.db",
"LINQPad.User.TypedDataContext"));
var customerCount = dc.Customers.Count();
这应该可以帮助您入门.请记住您所链接的答案中所述的警告!
This should get you started. Bear in mind the caveats, as stated in the answer to which you linked!
这篇关于如何在Visual Studio中使用LinqPad生成的上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!