# Data Entity

To be able to register Data Repositories in Unit of Work, an Entity file needs to be created and registered in the Data Context file, so that it can be referred to that Entity in the Unit of Work files. In the following, the process of creating an Entity for a Data Table and registering it in the Data Context file will be described.

## Entity file

Our applications are following a code-first Entity Framework, so for every Data Table, an Entity object representing their structure needs to be created. In the following, the process of creating an Entity class for a Data Table will be described:

1. Right-click on the "Entities" Folder under the "Data" directory, click "Add", then click "New Item".
2. Type the name of the Data Table (in singular form) as name for the class, then click "Add".
3. For each column of the Data Table, create a public Property of the same name with the corresponding data type. (For Dates, use "DateTime?" as data type).
4. For the column that uniquely identifies the record, add "\[Key]" above the Property declaration.
5. For columns that store identifiers to map records of this Data Table to a corresponding record in another Data Table, add "\[ForeignKey("...")]" above the Property declaration and declare an additional Property that is public, virtual and has the name of the other Data Table as data type. The name you give to this additional Property should be inserted in the brackets of "\[ForeignKey("...")]".
6. Register the repository in "DataContext.cs"

In the following, a specific example for each of the different Property types are shown to illustrate the exact implementation:

### Example 1: Basic Properties

```
public string Location { get; set; }

public DateTime? WorkDate { get; set; }
```

### Example 2: Key Properties

```
[Key]
public string ID { get; set; }
```

### Example 3: Foreign Key Properties

```
[ForeignKey("Station")]
public string StationID { get; set; }

public virtual StationHeader Station { get; set; }
```

## Data Context

"DataContext.cs" is the Adapter used by a Data Repository to make a transaction to the specific data source and is found in the "Access" folder under the "Data" directory of an application. Only when a class is registered in this file, it will be recognized as Data Entity.

The class includes a Constructor that initializes a Dictionary Property, methods related to the creation of the Data Context as well as regions where the Data Repositories are stored in categories.

To register a Data Repository, insert the following line of code in the respective region ("ADMINISTRATOR" for System-related Data Repositories or "BUSINESS-OBJECTS" for any other Data Repository):

```
public virtual DbSet<NameOfEntity> NameOfEntities { get; set; }
```

Notice the convention that the Property Name ("NameOfEntities") is the plural of the Data Type ("NameOfEntity"), which is the Entity class corresponding to the Data Repository.

Additionally, within each region they are categorized in groups that are visibly separated by more empty lines, so the newly added repository should be placed in the group it belongs to.

## Example

To demonstrate the overall procedure, it will be shown how an Entity file is created and registered in the Data Context for the following Data Table "CapacityPlans":&#x20;

<figure><img src="https://4150968783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FE2jCdhU9OpCbUJl0tqSd%2Fuploads%2FcbfhfMICFIQbQKjBvQb4%2Fimage.png?alt=media&#x26;token=e41727cc-bc77-4cbb-a770-8cffd0aa1b31" alt=""><figcaption></figcaption></figure>

After creating a new class file in the "Entities" folder naming it "CapacityPlan" (which is the singular of "CapacityPlans"), the following code is inserted based on the columns of the "CapacityPlans" Data Table:

```
namespace Advantech.Ctos.Data.Entities
{
    public class CapacityPlan
    {
        [Key]
        public string ID { get; set; }

        public DateTime? WorkDate { get; set; }

        public double Capacity { get; set; }

        public double Utilization { get; set; }

        public double Quota { get; set; }

        public string ChangedBy { get; set; }

        public DateTime? ChangedDate { get; set; }

        public string Location { get; set; }

        [ForeignKey("Station")]
        public string StationID { get; set; }

        public virtual StationHeader Station { get; set; }
    }
}
```

Then openeing the "DataContext.cs" file, the following line of code is inserted:

```
public virtual DbSet<CapacityPlan> CapacityPlans { get; set; }
```

