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 :-)
CodePudding user response:
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.
CodePudding user response:
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
TypedDataContext
with my own ContextSearch and replace
namespace LINQPad.User
with 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.