Documentation

Creating Resources

← All docs

Creating Resources

Add resources to the FGA hierarchy.

CreateResource is a convenience extension on ISqlOSFgaDbContext that creates an SqlOSFgaResource and adds it to the change tracker.

Usage

// Auto-generated ID
var resourceId = context.CreateResource(
    parentId: "retail_root",
    name: "Walmart",
    resourceTypeId: "chain"
);

// Custom ID
var orgResourceId = context.CreateResource(
    parentId: "root",
    name: "Acme Corp",
    resourceTypeId: "organization",
    id: $"org::{org.Id}"
);

Method signature

public static string CreateResource(
    this ISqlOSFgaDbContext context,
    string parentId,
    string name,
    string resourceTypeId,
    string? id = null);
ParameterDescription
parentIdParent resource ID in the hierarchy
nameDisplay name
resourceTypeIdResource type (e.g., chain, location)
idOptional. If null, a GUID is generated

Returns the resource ID (generated or custom).

Transaction behavior

CreateResource adds the resource to the EF Core change tracker but does not call SaveChangesAsync. Save it alongside your domain entity in one transaction:

var resourceId = context.CreateResource("retail_root", request.Name, "chain");

var chain = new Chain { ResourceId = resourceId, Name = request.Name };
context.Chains.Add(chain);

await context.SaveChangesAsync(); // saves both the resource and the chain