Does anyone know if it's possible to build a model in Linqpad 7 from the data of a table or view (EF connection)? I have a table with hundreds of columns.. would be nice just to right click it and chose "build model" (or rightclick on a .Dump() and build it from there). I mean it has all the data, just need to export the column names and datatypes :-)
-
1@Yitz That's awesome, more or less exactly what I needed :-) Copy the model from ILSpy and do some replacing to neat it up. Thank you. Please add that as an answer and I'll accept it.steb– steb2022-09-06 10:19:57 +00:00Commented Sep 6, 2022 at 10:19
-
Added as answer and deleted commentYitz– Yitz2022-09-06 13:09:34 +00:00Commented Sep 6, 2022 at 13:09
3 Answers
You could try using the Query => Reflect Query in ILSpy menu option.
Just type var a = new MyTableType(); and click through the MyTableType type to find the code which EF creates.
1 Comment
I've done this recently a few times with Sqlite databases and also a while back with a SQL database.
I wanted to be able to use the dll in Visual studio and didn't want to depend on Linqpad, but did want to to be able to use it in Linqpad.
You don't have to do it this way, but the the process I used was something like this.
Select the database in LinqPad and run the script
Util.OpenILSpy(typeof(TypedDataContext));as a C# Expression.In ILSpy, scroll up to the Assembly tab and right click on the dll which will have a name starting with TypedDataContext_ (see example screenshot)

Select Save Code from the context menu.
Open the selected folder and browse to the Subfolder LinqPad\User.
Copy the template csproj file into that folder and then open the project file. (Note ILSpy will have generated its own csproj file and you can use that and ignore the rest of the instructions if you don't mind a dependency on Linqpad)
Open TypedDataContext.cs
Remove the line
using LINQPad.Drivers.EFCore;Search for and replace
ConnectionHelper.CurrentCxString ?? Util.CurrentCxString;with a empty string.Search and replace
TypedDataContextwith my own ContextSearch and replace
namespace LINQPad.Userwith my own namespace.Compile.
The csproj file template for Sqlite projects was
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="6.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="6.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.8" />
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.EntityFrameworkCore">
</Reference>
<Reference Include="Microsoft.EntityFrameworkCore.Proxies">
</Reference>
<Reference Include="Microsoft.EntityFrameworkCore.Sqlite">
</Reference>
<Reference Include="Microsoft.EntityFrameworkCore.Relational">
</Reference>
</ItemGroup>
</Project>
and I think the one for SQL was
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="6.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.8" />
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.EntityFrameworkCore">
</Reference>
<Reference Include="Microsoft.EntityFrameworkCore.Proxies">
</Reference>
<Reference Include="Microsoft.EntityFrameworkCore.Relational">
</Reference>
</ItemGroup>
</Project>
Obviously this depends on net6.0 and a specific version of EF, but you get the idea.
1 Comment
To a degree, you can automate this too using the package `ICSharpCode.Decompiler`.
In my LINQPad query I added a connection to a local SQLite database.
Batches is a class auto-generated by LINQPad using an EF Core connection.
string dll = typeof(Batches).Assembly.Location;
var decompiler = new CSharpDecompiler(dll, new DecompilerSettings { ThrowOnAssemblyResolveErrors = false});
var fullTypeNames = decompiler.TypeSystem.MainModule.TypeDefinitions
.Where(x => x.FullTypeName.Name == "Batches")
.Select(x => new {
type = x.FullTypeName,
code = decompiler.DecompileTypeAsString(x.FullTypeName)
})
.ToList()
;
fullTypeNames.Dump();
output looks like
2 Comments
batches is the name of a table in my database that LINQPad is connected to.