Classifying personal data

This page covers every attribute you put on an entity to tell Custodian what personal data it holds. The classification says what the data is, why you hold it, on what lawful basis, for how long, and what happens when that time is up. Everything else in this guide follows from it. The examples come from the Shop and Clinic samples.

1. Mark the entity

[PersonalDataEntity] marks a class as personal data and declares one purpose for it. [DataSubject] names the property that links each row to the person it is about. [PersonalData] marks each personal property with its category.

[PersonalDataEntity(
    Purpose = Purposes.Billing,
    LawfulBasis = LawfulBasis.LegalObligation,
    Retention = "P7Y",
    Anchor = RetentionAnchor.CreatedAt,
    OnExpiry = ExpiryAction.Anonymise)]
public sealed class Order
{
    public int Id { get; set; }

    [DataSubject]
    public Guid CustomerSubjectId { get; set; }

    [PersonalData(DataCategory.Identity)]
    public string? BillingName { get; set; }

    [PersonalData(DataCategory.Location)]
    public string? BillingAddress { get; set; }

    public DateTimeOffset PlacedAt { get; set; }

    public decimal Total { get; set; }

    public List<OrderLine> Lines { get; set; } = [];
}

The attribute takes five values.

Property Meaning
Purpose The name of the processing purpose. Keep the names in one class of constants, as Purposes does.
LawfulBasis The Art. 6 basis for that purpose. One of Consent, Contract, LegalObligation, VitalInterests, PublicTask, LegitimateInterests.
Retention How long the row is kept, as an ISO-8601 duration such as P7Y or P30D.
Anchor The moment the period counts from. CreatedAt is the default.
OnExpiry What happens when the period ends. Delete is the default.

Anchor is one of the following.

OnExpiry is one of the following.

Custodian checks the classification when the application starts. An entity with no purpose, a purpose with no lawful basis, or a retention that is not an ISO-8601 duration stops the application. Retention covers the worker that acts on expiry, and how configuration can change a period without a release.

Exactly one property carries [DataSubject]. It holds the id the subject registry gave the person. An entity with none, or with more than one, stops the application at startup.

The link is what makes the rights work. An access request, an erasure and a legal hold all find a person's rows through it. Data-subject rights covers them.

Order has no foreign key to Customer. That is deliberate. The order must outlive the customer account, because bookkeeping law requires it for seven years and the account goes when the customer asks. So the order points at the data subject, not at the account row. Erasing a customer deletes the account and leaves the order with its amount and without the payer's name and address. See Erasure_deletes_the_customer_account and Erasure_keeps_what_was_paid_and_removes_the_payers_name_and_address.

3. Declare more than one purpose

Repeat [PersonalDataEntity] when the same rows serve several purposes. Each purpose has its own lawful basis and retention. The Clinic holds a practitioner's details for employment and shows the name when booking.

[PersonalDataEntity(Purpose = Purposes.Employment, LawfulBasis = LawfulBasis.Contract,
                    Retention = Retention.StaffRecords, Anchor = RetentionAnchor.LastActivity, OnExpiry = ExpiryAction.Delete)]
[PersonalDataEntity(Purpose = Purposes.Scheduling, LawfulBasis = LawfulBasis.Contract,
                    Retention = Retention.StaffRecords, Anchor = RetentionAnchor.LastActivity, OnExpiry = ExpiryAction.Delete)]

The retention periods are constants here, so every entity that belongs to the medical record or the staff record names the same one. The Retention class in ClinicPrivacy.cs holds them.

When data has a different lawful basis and a different lifetime, make it a separate entity. The Shop keeps newsletter consent in MarketingPreference, apart from Customer. Its basis is consent and it is asked again every year. Withdrawing consent then ends the newsletter and leaves the account alone, as Withdrawing_marketing_consent_keeps_the_customer_account shows.

Reserve a field for some of those purposes

By default every personal property is open to all the purposes its entity declares. Purposes on [PersonalData] narrows one property. No sample needs this, so the example is the model Custodian's own EF Core tests use. A Member is kept for care and for billing, and the diagnosis is for care only.

/// <summary>Classified by attributes. Care keeps it a year after the last visit; billing seven years by law.</summary>
[PersonalDataEntity(Purpose = Purposes.Care, LawfulBasis = LawfulBasis.Contract, Retention = "P1Y", Anchor = RetentionAnchor.LastActivity)]
[PersonalDataEntity(Purpose = Purposes.Billing, LawfulBasis = LawfulBasis.LegalObligation, Retention = "P7Y", Anchor = RetentionAnchor.CreatedAt)]
public sealed class Member
{
    public int Id { get; set; }

    [DataSubject]
    public Guid SubjectId { get; set; }

    [PersonalData(DataCategory.Identity)]
    public string Name { get; set; } = "";

    [PersonalData(DataCategory.Contact), Encrypted, BlindIndex]
    public string Email { get; set; } = "";

    [PersonalData(DataCategory.Identity, Purposes = [Purposes.Care]), SpecialCategory]
    public string? Diagnosis { get; set; }

    public int Visits { get; set; }
}

A clerk who works for billing can still create the row, as long as the reserved field is left empty.

[Theory, EachProvider]
public async Task A_clerk_adds_a_member_for_billing_and_leaves_the_care_only_diagnosis_empty(DatabaseProvider provider)
{
    await using var clinic = await CreateAsync(servers, provider);
    using var clerk = clinic.As(Roles.Clerk, Purposes.Billing);

    await clinic.Add(new Member { SubjectId = Guid.NewGuid(), Name = "Ada", Email = "ada@example.org" });

    Assert.Equal(1, await clinic.Count("SELECT COUNT(*) FROM members"));
}

When a whole piece of data serves a narrower purpose and has its own lifetime, a separate entity is simpler. The Clinic does that for the reason for a visit. See Health data and break-glass.

4. Categorise each personal property

[PersonalData(category)] marks a property as personal and says what kind of data it is.

A pseudonymous identifier is still personal data. ActorId on Practitioner is the id a practitioner signs in with, and it is marked Identity.

A property without an attribute is not treated as personal. Order.Total, Order.PlacedAt and Practitioner.Specialty are examples. An analyst who may not see who bought something still sees the amount. See An_analyst_sees_what_was_sold_but_not_who_bought_it.

5. Encrypt and index

[Encrypted] encrypts the property with the data subject's own key. [BlindIndex] adds a keyed-hash column next to it, so the value can still be found by equality.

/// <summary>Encrypted, and searchable by equality only, because it identifies the patient outside the clinic.</summary>
[PersonalData(DataCategory.Identity), Encrypted, BlindIndex]
public string NationalId { get; set; } = "";

[PersonalData(DataCategory.Contact), Encrypted]
public string Phone { get; set; } = "";

[PersonalData(DataCategory.Contact), Encrypted]
public string Email { get; set; } = "";

The stored column holds ciphertext, as A_national_id_is_stored_as_ciphertext shows. The front desk still finds the patient, as A_receptionist_finds_a_patient_by_national_id_although_it_is_stored_encrypted shows. An entity with an encrypted property needs a key store, or the application does not start. Encryption and blind indexes covers keys, lookups and crypto-shredding.

6. Mark special-category data

[SpecialCategory] marks Art. 9 data such as health data. It forces encryption and audits every read. On its own it also classifies the property as personal data, so [PersonalData] is not needed.

/// <summary>Who treats the patient. Special category: the link itself says the patient is being treated, often for what.</summary>
[SpecialCategory]
public Guid PractitionerSubjectId { get; set; }

/// <summary>Since when. Special category with the link it dates.</summary>
[SpecialCategory]
public DateTimeOffset Since { get; set; }

Neither property says [Encrypted], and both are stored as ciphertext. See Who_treats_a_patient_is_stored_as_ciphertext_because_the_link_reveals_health. A read is audited even when the reader is entitled to it. See Reading_clinical_notes_is_audited_even_for_the_treating_clinician.

The example also shows that a link can be special-category data. Who treats a patient reveals something about the patient's health. Health data and break-glass covers the Clinic in full.

LawfulBasis records the Art. 6 basis only. The Art. 9(2) condition that special-category data also needs has no place in the model. The Clinic states it in the comment on each purpose in ClinicPrivacy.cs.

Art. 7(1) requires you to show that a person consented. That proof must outlive the consent itself. The Shop keeps it in its own entity, with its own purpose and a legal-obligation basis.

[PersonalDataEntity(
    Purpose = Purposes.ConsentProof,
    LawfulBasis = LawfulBasis.LegalObligation,
    Retention = "P3Y",
    Anchor = RetentionAnchor.CreatedAt,
    OnExpiry = ExpiryAction.Delete)]
public sealed class ConsentRecord
{
    public int Id { get; set; }

    [DataSubject]
    public Guid SubjectId { get; set; }

    /// <summary>The purpose consented to, for example <see cref="Purposes.Marketing"/>. Not personal on its own.</summary>
    public string Purpose { get; set; } = "";

    /// <summary>The version of the consent text the customer saw. Not personal on its own.</summary>
    public string Wording { get; set; } = "";

    /// <summary>When the customer gave consent.</summary>
    [PersonalData(DataCategory.Behavioural)]
    public DateTimeOffset GivenAt { get; set; }
}

The entity holds the subject link and no name, address or email. It is pseudonymous. Purpose and Wording are not personal on their own, so they carry no attribute. The time of consent is.

The proof survives both ways the consent can end. See Withdrawing_marketing_consent_keeps_the_proof_that_it_was_given and The_consent_to_the_newsletter_can_still_be_proven_after_the_preference_has_expired.

8. Leave non-personal entities alone

An entity without [PersonalDataEntity] is not personal data. Custodian adds nothing to it. It has no subject, no tenant column, no row-level security, no retention and no audit. Product and OrderLine are the Shop's examples.

An order line is left unmarked because a SKU and a price say nothing about anyone without the order they belong to.

Next