问题
I have an EF4.1 database first application, and the .ToString() method is only giving me the reflected type, and not the SQL query.
How do I make my application show me the Linq query in SQL format?
I've added version 4.1 of C:\Program Files (x86)\Microsoft ADO.NET Entity Framework 4.1\Binaries\EntityFramework.dll
to my project. I'm guessing the ToString feature is added by an extension method, but even when I add "Using system.data.Entity" I'm unable to getthe string output to work.
Does this mean ToString only works for Code First deployments?
public IEnumerable<CompanyDetail> GetAllCompanies(int? startPage, int? stopPage, string Filter, int? MaxResults)
{
var t = from c in _entities.CompanyDetail
select c;
string test = t.ToString();
var t2 = _entities.CompanyDetail;
string test2 = t2.ToString();
return t.ToList();
}
回答1:
I can confirm that it works for database first applications.
I am guessing your code is still using the EF 4 classes since you didn't mention adding the EF 4.1 DbContext generator to your model. If that's the case, you can right-click on the model, choose "Add Code Generation Item..." and select the ADO.NET C# DbContext Generator. If you don't see it in your installed templates, look for it under Online Templates.
来源:https://stackoverflow.com/questions/8129970/showing-sql-command-with-ef4-1s-tostring