Typed Entities - rezanid/xrmtools GitHub Wiki

Typed Entities refer to actual C# classes that represent Dataverse entities (tables) with proper properties for each attribute, instead of using the generic Entity class with string keys. Xrm Tools can auto-generate these classes for you based on your Dataverse environment’s metadata.

To generate typed entity classes, you simply add an assembly-level attribute to your project listing the entities (and optionally which attributes) you want to generate. For example, create a new C# file in your project (you might call it Entities.cs) and add:

[assembly: Entity("contact", AttributeNames = "firstname,lastname")]

When you save this file, Xrm Tools will connect to your Current Environment, retrieve the metadata for the Contact entity (in Dataverse), and generate a C# class for it. By default, it will use the entity’s display name or schema name (configurable) to name the class – in this case, likely Contact. The class will include properties for all the attributes you listed (FirstName, LastName), and possibly others by default (like Id, etc.). If you omit AttributeNames, Xrm Tools can generate the class with all attributes, but it’s usually better to specify the subset you actually use, for clarity and to reduce noise.

Having a Contact class in code means your plugins can work with Contact objects directly, benefiting from compile-time checking and IntelliSense on attributes, rather than using the late-bound Entity["fieldname"] approach.

You can add multiple [assembly: Entity("logicalName", ...)] lines (one per entity you want as a class). Common practice is to gather these in one file. For instance:

[assembly: Entity("account")]
[assembly: Entity("contact", AttributeNames = "firstname,lastname")]
[assembly: Entity("new_customentity", AttributeNames = "new_field1,new_field2")]

Each entry will result in a class (Account, Contact, New_customentity in this example). Include only the attributes you need in AttributeNames for each, to keep the classes focused.

[!Note]

In the current version of Xrm Tools, the [Entity] attribute is only allowed at the assembly level. A future version may allow applying EntityAttribute to an actual class definition, which would let you name the class yourself. For now, the class name is determined by the Dataverse metadata (either the schema name or the display name of the entity). The extension may provide an option to choose which naming convention to use. Keep your extension up to date for new features like this.