# SQL storage policies
`Ark.Tools.Compliance.Sql` turns classification into SQL Server DDL: sensitivity
classification (`ADD SENSITIVITY CLASSIFICATION`) and dynamic data masking
(`ADD MASKED WITH`). Generation is **opt-in per type** — C# names are not the SQL
schema, so nothing is emitted unless you say which table and columns you mean.
```xml
```
Requires `EnableArkToolsCompliance=true` on the project that declares the entities.
## Declaring the mapping
Layer the SQL attributes *on top of* the classification:
```csharp
using Ark.Tools.Compliance;
using Ark.Tools.Compliance.Sql;
[SqlDataPolicy(Schema = "sales", Table = "Customers", Label = "Confidential - GDPR")]
public sealed record CustomerEntity
{
[PersonalData]
[SqlColumnPolicy("email_address", StoragePolicy.Masked,
MaskFunction = SqlMask.Email, InformationType = "Contact Info")]
public EmailAddress Email { get; init; }
[PersonalData]
[SqlColumnPolicy("phone_number", StoragePolicy.Masked)]
public PhoneNumber? Phone { get; init; }
}
```
- `[SqlDataPolicy]` turns generation **on** for the type. `Schema` defaults to the
SQLCMD token `$(ComplianceSchema)` and `Label` to `$(ComplianceLabel)` so they can
be substituted per environment; `Table` has no default — it must be explicit.
- `[SqlColumnPolicy(columnName, storagePolicy)]` carries the **column name
verbatim**; the generator never derives it from the property name. `Schema`,
`Table`, and `Label` may be overridden per member for split-table mappings;
`InformationType` feeds the classification metadata; `MaskFunction` picks the
masking function (`SqlMask.Default` = `default()`, `SqlMask.Email` = `email()`,
or any literal SQL masking function string).
- `StoragePolicy`: `None` (classify only), `Masked` (classification + dynamic data
masking), `ApplicationEncrypted` (classification; value encrypted by the
application before storage).
- Inside a `[SqlDataPolicy]` type, every classified member **must** declare a
`[SqlColumnPolicy]` — a missing one is error `ARKPII007`. Types without
`[SqlDataPolicy]` generate nothing; they remain covered by the catalog and sink analyzers.
## What gets generated
During build, the package's targets expand the generator output into one
`policy-.compliance.sql` file per type under
`$(IntermediateOutputPath)ArkCompliance.Sql` (override with
`ArkComplianceSqlOutputPath`). Content:
```sql
ADD SENSITIVITY CLASSIFICATION TO [sales].[Customers].[email_address]
WITH (LABEL = 'Confidential - GDPR', INFORMATION_TYPE = 'Contact Info', RANK = HIGH);
ALTER TABLE [sales].[Customers]
ALTER COLUMN [email_address] ADD MASKED WITH (FUNCTION = 'email()');
```
The scripts are *templates*: `$(Token)` placeholders left in them are ordinary
SQLCMD variables. You can substitute at build time with MSBuild items:
```xml
```
Unsubstituted tokens survive to deployment time and are resolved by
SqlPackage/`sqlcmd` variables instead.
## Wiring a database project (.sqlproj)
A Microsoft.Build.Sql database project consumes the generated policies as a
post-deployment script. Three pieces:
**1. A custom target** that asks each entity project for its policies (via the
package's `ArkGenerateComplianceSqlStandalone` entry point — it compiles the
project first, so it works on clean builds, and returns the generated scripts)
and concatenates them. The entity projects set `EnableArkToolsCompliance=true`
themselves:
```xml
$(MSBuildProjectDirectory)\obj\ArkCompliance.Sql
```
Do not pass `Properties` to the `` call. Extra global properties make
MSBuild build the entity project and its whole reference graph a second time,
into the same `obj` folders. The two builds then overwrite each other's outputs,
so every later build recompiles them. `WriteOnlyWhenDifferent` keeps the
concatenated file's timestamp, so an unchanged database project stays up to date.
The policies are generated per target framework. If an entity project sets
`TargetFrameworks`, pass only the framework to read, for example
`Properties="TargetFramework=net10.0"`. That matches the inner build MSBuild
already runs for that framework, so it does not cause a second build.
**2. The post-deployment script** includes the concatenated file:
```sql
-- Script.PostDeployment.sql
:r .\obj\ArkCompliance.Sql\CompliancePolicies.sql
```
```xml
DoNotCopy
```
**3. Optional: masked/privileged readers.** Dynamic data masking is enforced per
principal — grant `UNMASK` only to roles that need cleartext:
```sql
IF DATABASE_PRINCIPAL_ID(N'CompliancePrivilegedReader') IS NULL
CREATE USER [CompliancePrivilegedReader] WITHOUT LOGIN;
GRANT UNMASK TO [CompliancePrivilegedReader];
```
With this in place, `dotnet build` on the database project regenerates the policy
scripts from the current entity annotations and packs them into the DACPAC —
classification and masking deploy with every schema deployment, and a removed
`[SqlColumnPolicy]` removes its script on the next build.