Work with Visual Studio and script classes to create machine learning models.

How-to ExamplesFeatureApplication → Tasks and Classes



Solution (.dbsln) and model (.mlnet): MLNET-ModelBuilder

Visual Studio Files: myMLApp


Please ensure that the solution and the model are located in the same folder when running the application. The model’s output will be displayed in the TraceWindow.






Summary

Model Builder is a Visual Studio extension that lets you build, train, and generate code to consume ML models through a guided UI (AutoML-backed).

In this example, we created a binary sentiment model to predict whether a restaurant customer review is Positive or Negative, then integrated the generated consumption code into FrameworX Multiplatform.

Official website of the tool: ML.NET Model Builder - machine learning in Visual Studio | .NET


Technical Information

Requirements

  • Visual Studio 2026

  • .NET 8.0

  • .NET 10.0

  • FrameworX Multiplatform

How the model was created (high level)

We followed Microsoft’s ML.NET Tutorial - Get started in 10 minutes | .NET “get started” flow and trained a model for sentiment classification. The key benefit: Model Builder generates the training/consumption code and the model file, so integration is mostly about copying the generated consumer code and loading the .mlnet model.

Integration

Copy the code below into Scripts / Tasks. This code is used to consume models inside the Scripts module. Once added, you can feed the model using your process variables in real time and read the model outputs for use in your logic.

All the code below was generated by the Model Builder tool, no changes were required. Just copy and paste it into your solution.

Define the Inputs and Outputs

The input schema must match what the model was trained with (column order/types). Keep the same structure you got from the generated project.

public class ModelInput {
    [LoadColumn(0)]
    [ColumnName(@"col0")]
    public string Col0 { get; set; }

    [LoadColumn(1)]
    [ColumnName(@"col1")]
    public float Col1 { get; set; }
}

public class ModelOutput {
    [ColumnName(@"PredictedLabel")]
    public float PredictedLabel { get; set; }
}

Notes

  • Define all input columns the model expects.

  • For outputs, you only need to define what you will actually use (e.g., PredictedLabel).

Implement the consumption methods

Declare global variable and initialize the constructor

public static readonly Lazy<PredictionEngine<ModelInput, ModelOutput>> PredictEngine = new Lazy<PredictionEngine<ModelInput, ModelOutput>>(() => CreatePredictEngine(), true);

private static PredictionEngine<ModelInput, ModelOutput> CreatePredictEngine() {
    var mlContext = new MLContext();
    ITransformer mlModel = mlContext.Model.Load(@"C:\Users\eduar\source\repos\myMLApp\myMLApp\SentimentModel.mlnet", out var _);
    return mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
}


Create the prediction method:

public static ModelOutput Predict(ModelInput input) {
    var predEngine = PredictEngine.Value;
    return predEngine.Predict(input);
}

Create the Main method

public async Task Main() {

    var sampleData = new ModelInput() {
        Col0 = "This restaurant was bad."
    };
    
    var result = SentimentModel.Predict(sampleData);
    var sentiment = result.PredictedLabel == 1 ? "Positive" : "Negative";

    @Info.Trace($"Text: {sampleData.Col0}");
    @Info.Trace($"Sentiment: {sentiment}");
}


You can use any LLM to assist during implementation. For this implementation, we used Visual Studio with Claude Code.



In this section...