Code Generation (Extension classes)


Main purpose of these classes- is to be used as table rows instead of generic BGEntity class.

This is the main type of Code Generation. It removes all boilerplate code and provides super easy and slim API for accessing table rows and their fields.

Advantages of using CodeGen

  1. No boilerplate code
  2. Fastest available method for accessing field values
  3. Compile time check (if field names are changed you'll get compilation error)


Accessing fields without CodeGeneration

Accessing fields with CodeGeneration

Accessing rows without/with CodeGeneration

More Details

Generated classes are inherited from generic BGEntity class. These classes can not be added to GameObject, cause they are not Unity's MonoBehaviour (there are also another generated classes, which can be added to GameObjects, learn more here). When this CodeGen is enabled, database populated not with basic BGEntity objects, but with these extension classes. So when you access database like this var entity=meta[0]; entity variable has this extension type with all the fields generated as properties.

Additionally, you will get all main table(meta) methods alternatives as static methods of that particular class, like this:

public static BGE_MyTable FindEntity(Predicate<BGE_MyTable> filter)
public static List<BGE_MyTable> FindEntities(Predicate<BGE_MyTable> filter)
public static void ForEachEntity(Action<BGE_MyTable> action, Predicate<BGE_MyTable> filter=null)
public static BGE_MyTable GetEntity(BGId entityId)
public static BGE_MyTable GetEntity(int index)
public static BGE_MyTable NewEntity()

so instead of this, for example:

    var repo = BGRepo.I;
    var meta = repo["MyTable"];
    BGE_MyTable entity = (BGE_MyTable) meta[new BGId("HThOdRCxlEGU1aefSG+Nbw")];
you can write code like this:
    BGE_MyTable entity = BGE_MyTable.GetEntity(new BGId("HThOdRCxlEGU1aefSG+Nbw"));

using generated BGE_MyTable class.

Generate a property for each entity (optional)

Additionally, you can generate a C# property for each row(entity) and use this property to access the corresponding row directly, without the need to locate/find it first.


  • "Entities prefix" should be defined in CodeGen addon settings (for example KEY_). CodeGen addon settings is located here: BGDatabaseWindow->Addons->CodeGen
  • Each table, you want entities properties to be generated for, should have "Entity name is unique" toggle on under (Configuration->[TableName]) and "Entity name is empty" toggle off.
  • Each entity should have empty or unique name, containing letters, numbers, underscores, spaces and dots only. Entities with empty names will be skipped. Dots and spaces will be converted to underscores
  • Rows count (with not empty name) should be under 10000 rows

Once the properties are generated you can access particular row like so:

    BGE_MyTable entity = BGE_MyTable.KEY_MyUniqueKey;

, where MyUniqueKey is the name of the row and KEY_ is entities prefix from CodeGen addon settings.

If this option is used, there are additional cases when you need to regenerate extension classes: if you add/delete the rows with names or if you change rows names.

Keys method generation

If you create custom keys for you table, code generator generates additional methods for accessing row(s) by each key with following naming convention: GetEntityByKey{Key name}/GetEntitiesByKey{Key name}.

How to enable/disable

1) Open BGDatabase window from Unity menu Window->BGDatabase

2) Chose Addons->CodeGen

3) Toggle enabled on and fill all parameters:

  1. Code Generator - leave it as it is
  2. Source file - all generated classes will use one single .cs file. You can place it anywhere in your project, but not under Editor folder
  3. Package - package (namespace) for you classes.
  4. Classes names prefix - prefix for your classes names. For example you can use prefix like D_ to distinct your database classes from the rest of the project classes
  5. Field names prefix - the same as classes names prefix, but for fields.
  6. Entities prefix (optional) - fill it if you want to generate a property for each entity (read more above)

After you fill all parameters, press Generate button to generate classes.

Note, you should regenerate these classes after any change to database structure (e.g. if you add/remove tables or fields, changing their names/order etc.)

Generated classes extensions

All generated classes are partial, so you can create your own class with the same namespace/name and add additional methods manually to these classes