Creating classes from your content model
Tags: visualstudio, content, cms, sdk
Published: 2018-06-27 13:45:49

---
title: Creating classes from your content model
published: true
description: .NET tool related to classes and the content model
tags: visual studio, content, cms, sdk
canonical_url: https://www.contentful.com/blog/2018/06/27/tool-creating-classes-content-model/
---

![Creating classes from your content model](https://images.ctfassets.net/fo9twyrwpveg/5TFXhevTkkIQi06s20gm4g/81790f0d8e0423db1027dfaeb71539d3/27-June-Classes-from-your-content-model-update.png)

In my [previous article](https://www.contentful.com/blog/2017/11/14/how-you-can-treat-your-content-model-as-code-with-.net/?utm_campaign=classes-content-model&utm_medium=referral&utm_source=devto&utm_content=classes-content-model&utm_term=), I showed how to use the Contentful.CodeFirst package to let your C# classes act as the basis of your content model in Contentful. But what happens if your content model already exists and you want to create corresponding, strongly-typed C# models? That's a lot of boilerplate code to write—today I'm happy to release a new tool that helps you do just that. 

With the release of .NET Core 2.1, "global tools" is a new feature that will become generally available. This means that you can run `dotnet tool install -g your-tool-id` and install any tool directly from NuGet for it to become available to call from your command line.

To install the model creator tool, make sure you have the latest [.NET Core SDK](https://www.microsoft.com/net/download/dotnet-core/) version installed and run `dotnet tool install -g contentful.modelscreator.cli` from your command line. 

You should now be able to run `contentful.modelscreator.cli --help` to list all the available commands. Only the access token and space ID are mandatory, but there are a few other commands that are interesting:

- The `--namespace` or `-n` option allows you to specify the namespace in which the classes should be created.
- The `--path` or `-p` option allows you to specify where the classes should be created on your hard drive.

If you test it with credentials from our Example App, you get the following result: 

~~~
contentful.modelscreator.cli -s qz0n5cdakyl9 -a df2a18b8a5b4426741408fc95fa4331c7388d502318c44a5b22b167c3c1b1d03
~~~

![Initial result when tested with credentials from Example App](//images.ctfassets.net/fo9twyrwpveg/1uEbm5HDTSUki6i8EgwGS/69a787fc59d0251c342f07ff46e50cd8/first-result.PNG)

What’s happened is that the classes have been created in my current directory where, for example, the Course class looks like this:

~~~csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Contentful.Core.Models;

namespace Replace.Me.NameSpace
{
    public class Course
    {
        public SystemProperties Sys { get; set; }
        public string Title { get; set; }
        public string Slug { get; set; }
        public Asset Image { get; set; }
        public string ShortDescription { get; set; }
        public string Description { get; set; }
        public int Duration { get; set; }
        public string SkillLevel { get; set; }
        public List<Lesson> Lessons { get; set; }
        public List<Category> Categories { get; set; }
    }
}
~~~

It automatically resolves the field types to the correct type and sets the name of the property to correspond with the id of the field in Contentful. It also tries to resolve reference field to the correct generic type, `List<Lesson>` and `List<Category>` for example, by inspecting the content type validations of the field. If there's a single content type validation, the class that content type corresponds to will be used as the generic type of that property.

As seen in the class above, the namespace will be set to "Replace.Me.NameSpace" if it's not provided in the call. This can then be easily searched and replaced.

To set the namespace in the initial call, just alter it like so...

~~~
contentful.modelscreator.cli -s qz0n5cdakyl9 -a df2a18b8a5b4426741408fc95fa4331c7388d502318c44a5b22b167c3c1b1d03 -n MyProject.Models
~~~

... and the namespace declaration will look like this:

~~~
namespace MyProject.Models
{
    public class Course
    {
          // Omitted for brevity
    }
}
~~~

That's the same way you can change the default path, which is the current directory, by using the -p parameter.

~~~
contentful.modelscreator.cli -s qz0n5cdakyl9 -a df2a18b8a5b4426741408fc95fa4331c7388d502318c44a5b22b167c3c1b1d03 -n MyProject.Models -p c:\temp
~~~

As a result, the classes will now be created at c:\temp.

If the path already contains the files, you will be prompted to overwrite (if you've made changes you want reflected in an existing class) or skip (if there are no changes) that file.

![Skip or overwrite, depending if there are changes](//images.ctfassets.net/fo9twyrwpveg/627NbYxr4koWIGgOamsG6i/86224e8badaaf8ff33f6fc2e67c70523/skip-result.PNG)

If you want to automatically overwrite files in the directory, you can use the `--force` or `-f` command and the files will be overwritten without any prompts.

## Leaving the command line

Not everyone is crazy about using the command line and would rather have something in the IDE or editor of their choice. The CLI tool above is available as a plugin to both Visual Studio and Visual Studio Code.

To install it in Visual Studio, click Tools --> Extensions --> Updates, search for Contentful and install the Contentful.ModelsCreator plugin.

After successfully installing the plugin, a new number of settings can be found under Tools --> Options. 

![Visual Studio settings to install the Contentful plugin](//images.ctfassets.net/fo9twyrwpveg/1hLeAAQFvyeimaWoUcoIKy/163fc022cdc95a9fe991d243a7e8d8ff/vs-settings.PNG)

Once the access token and space id have been set, I can now right click any folder in my solution and the models will be created there.

![Models being created in Visual Studio](//images.ctfassets.net/fo9twyrwpveg/1owVkTy7hC0gyseWiecmaI/87b9728394a5d53acee1b74d458cefba/models-creator-vs.gif)

Similarly, you can install the plugin through the Marketplace in Visual Studio Code by clicking the Extensions tab and searching for Contentful. Once the extension is installed, there are a similar number of settings where you can specify the API key, space id and namespace.

![Visual Studio Code settings](//images.ctfassets.net/fo9twyrwpveg/2Y9O2BAO5aKMyQwoy6OeW/365fbff5389745faf23e1dfc4ef2fa3e/vs-code-settings.PNG)

After that, right click on any folder in Visual Studio Code and choose the Create Contentful Models option.

![Models being created in Visual Studio Code](//images.ctfassets.net/fo9twyrwpveg/1RhCsm84wogO6WMgiMAAak/dddbdee73fe3f529c332d5da78a53891/models-creator.gif)

## Discover more and use Contentful

In this article, we looked at a new CLI tool and plugins available to help you create strongly-typed C# classes directly from your Contentful content model. If you want to learn more, check out [the CLI](https://github.com/contentful/dotnet-models-creator-cli) and the [Visual Studio Code plugin](https://github.com/contentful/dotnet-models-creator-vscode) on our Github repository. You can also [read more about .NET Core 2.1](https://natemcmaster.com/blog/2018/05/12/dotnet-global-tools/)

If you're new to Contentful, you can easily [create an account for free](https://www.contentful.com/sign-up/?utm_campaign=classes-content-model&utm_medium=referral&utm_source=devto&utm_content=classes-content-model&utm_term=) to try what we've run through in this tutorial. Getting a space for your content to get your static site up and running is a breeze; try it for yourself!

Happy coding!