2

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 :-)

2
  • 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. Commented Sep 6, 2022 at 10:19
  • Added as answer and deleted comment Commented Sep 6, 2022 at 13:09

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Next best thing, if Linqpad doesn't have built in support for it :-) thanks!
2

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.

1 Comment

Thank you very much for you detailed way to export the models. It's more or less the same way as Yitz told me, to use ILSpy, didn't think of it my self. In my case I'm more interested in just copy larges models from ILSpy into VS without using any files. Thank you and have a great day!
0

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

enter image description here

2 Comments

Isn't "Batches" the name of a table in the database? I.e. not a class always generated by EF/Linqpad, which your wording suggests.
batches is the name of a table in my database that LINQPad is connected to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.