Home > database >  Build model from result in Linqpad
Build model from result in Linqpad

Time:09-07

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.

  1. Select the database in LinqPad and run the script Util.OpenILSpy(typeof(TypedDataContext)); as a C# Expression.

  2. 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) enter image description here

  3. Select Save Code from the context menu.

  4. Open the selected folder and browse to the Subfolder LinqPad\User.

  5. 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)

  6. Open TypedDataContext.cs

  7. Remove the line using LINQPad.Drivers.EFCore;

  8. Search for and replace ConnectionHelper.CurrentCxString ?? Util.CurrentCxString; with a empty string.

  9. Search and replace TypedDataContext with my own Context

  10. Search and replace namespace LINQPad.User with my own namespace.

  11. 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.

  • Related