-
Notifications
You must be signed in to change notification settings - Fork 2
ClassGenerator
Another major part besides the search is the Class generator which offers you the possibility to create a C# class out of a database table.
The interface is splitted in three sections:
- Left: The list with the available tables / table types
- Middle: The list with the columns of a table / table type
- Right: The options and the result of the generator
When you've selected the desired table / table type the columns will be shown in the middle. Now you are able to select all needed columns (you can select / unselect all columns with the buttoms below the list). If you wan't you can change the name of a column (Alias in the grid). When you've selected all needed columns you can set the options of the generator. The following are provided:
-
Modifier: The modifier of the class (possible values:
public
- default,internal
,protected
,protected internal
) - Class name: The name of the class (the name of the table will be choosen by default)
- Namespace: The desired namespace (optional)
-
Options
- Sealed class: Adds the sealed modifier
- Summary: Adds an empty summary to the properties / fields
- DB Model (EF): Creates a class which can be used with Entity Framework (needed attributes will be added)
-
Nullable enabled (.NET 6): Adds the
?
operator to properties where the column is nullable (astring
property / field will also be initialized withstring.Empty
) - Backing field: Creates a backing field for each property
-
Use 'SetProperty' method: Uses the
SetProperty
method of theObservableObject
(part of the CommunityToolkit.Mvvm)
When you've set all needed options you can hit the Generate button.
You're also able to generate a class from a SQL query. To do that, hit the button Generate from query.
In the dialog you can specify the query from which the class should be generated.
ATTENTION: Before the class will be generated the query has to be validated (Validate button in the bottom). However, only the syntax of the query is validated. It is not checked whether the specified query can really be executed.
For example, the following query is valid, but it's not allowed to have multiple properties within a C# class:
SELECT
Id,
SomeValue,
OtherId AS SomeValue
FROM
BlubTable;
For this reason, the following message is displayed after validation:
The generated class looks like this (this class will cause an error because the property SomeValue
exists twice):
/*
NOTE - Unique property names
----------------------------
In your SQL query are several columns with the same name.
Since this is not possible in C#, please adjust the column names
and run the process again.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SomeFancy.Namespace
{
internal sealed class BlubTable : ObservableObject
{
/// <summary>
/// Backing field for <see cref="Id"/>
/// </summary>
private int _id;
/// <summary>
/// Gets or sets the TODO
/// </summary>
public int Id
{
get => _id;
set => SetProperty(ref _id, value);
}
/// <summary>
/// Backing field for <see cref="SomeValue"/>
/// </summary>
private string _someValue = string.Empty;
/// <summary>
/// Gets or sets the TODO
/// </summary>
public string SomeValue
{
get => _someValue;
set => SetProperty(ref _someValue, value);
}
/// <summary>
/// Backing field for <see cref="SomeValue"/>
/// </summary>
private int? _someValue;
/// <summary>
/// Gets or sets the TODO
/// </summary>
public int? SomeValue
{
get => _someValue;
set => SetProperty(ref _someValue, value);
}
}
}
If you want to generate multiple classes at once, you can hit the Generate multiple button (on the left side below the list with the tables).
This is very much the same as the normal export instead that you're able to generate several class at once.
Note: All existing settings (alias, column selection, etc.) will be used during the class generation!
MsSqlToolBelt by InvaderZim | Home