Relationships

Attributes for defining entity relationships — many-to-one, many-to-many, cascade behavior, and display names.

M2M

Namespace: Spiderly.Shared.Attributes.Entity

Usage:

Indicates that the entity represents a helper table for a many-to-many (M2M) relationship.

Example:

[M2M]
public class RolePermission
{
    [M2MWithMany(nameof(Role.Permissions))]
    public virtual Role Role { get; set; }

    [M2MWithMany(nameof(Permission.Roles))]
    public virtual Permission Permission { get; set; }
}

M2MWithMany

Namespace: Spiderly.Shared.Attributes.Entity

Usage:

Marks a property in a many-to-many (M2M) relationship.

Example:

[M2M]
public class RolePermission
{
    [M2MWithMany(nameof(Role.Permissions))]
    public virtual Role Role { get; set; }

    [M2MWithMany(nameof(Permission.Roles))]
    public virtual Permission Permission { get; set; }
}

WithMany

Namespace: Spiderly.Shared.Attributes.Entity

Usage:

Specifies the collection navigation property name in a related entity for establishing a bidirectional relationship in Entity Framework.

Purpose:

This attribute is used to define the inverse navigation property in a relationship, enabling proper relationship configuration and navigation in both directions.

Example:

public class Course : BusinessObject<long>
{
    public virtual List<Student> Students { get; set; } = new();
}

public class Student : BusinessObject<long>
{
    [WithMany(nameof(Course.Students))]
    public virtual Course Course { get; set; }
}

CascadeDelete

Namespace: Spiderly.Shared.Attributes.Entity

Usage:

Implements cascade delete behavior in many-to-one relationships. When the referenced entity is deleted, all entities that reference it will automatically be deleted as well.

This attribute is useful when:

  • Child entities should not exist without their parent

Example:

public class Comment : BusinessObject<long>
{
    [DisplayName]
    public string Text { get; set; }

    [CascadeDelete] // When the Post is deleted, all its Comments will be deleted
    [WithMany(nameof(Post.Comments))]
    public virtual Post Post { get; set; }
}

SetNull

Namespace: Spiderly.Shared.Attributes.Entity

Usage:

Specifies that the property should be set to null when the parent entity is deleted. Apply this attribute to a many-to-one relationship property.

DisplayName

Namespace: Spiderly.Shared.Attributes.Entity

Usage:

Specifies which property should be used as the display name for an entity in UI elements:

  • When applied to a property: The property's value will be used to represent the entity
  • When applied to a class: The specified property's value will be used to represent the entity
  • If no property or class is marked with this attribute: The entity's 'Id' will be used

Example:

public class User : BusinessObject<long>
{
    [DisplayName]
    public string FullName { get; set; } // Will be used in dropdowns and lists
}

[DisplayName("Department.Name")] // Uses related entity's property
public class Employee : BusinessObject<long>
{
    public virtual Department Department { get; set; }
}

ComplexManyToManyList

Namespace: Spiderly.Shared.Attributes.Entity

Usage:

Generates an editable list UI for complex many-to-many relationships (junction tables with additional fields). Shows ALL entities from the "other side" with editable junction fields — no add/remove/reorder controls.

Also auto-generates GetDefault{Property}For{Entity} API endpoints for pre-population of junction records.

Warning:

This attribute loads all "other side" entities into the form. It is suitable for small sets (e.g., 3 warehouses), not for large sets (e.g., thousands of entities).

Example:

public class ProductVariant : BusinessObject<long>
{
    [DisplayName]
    public string Name { get; set; }

    [ComplexManyToManyList]
    public virtual List<ProductVariantWarehouse> ProductVariantWarehouses { get; } = new();
}

[M2M]
public class ProductVariantWarehouse
{
    [M2MWithMany(nameof(ProductVariant.ProductVariantWarehouses))]
    public virtual ProductVariant ProductVariant { get; set; }

    [M2MWithMany(nameof(Warehouse.ProductVariantWarehouses))]
    public virtual Warehouse Warehouse { get; set; }

    [GreaterThanOrEqualTo(0)]
    public int Stock { get; set; }
}

SimpleManyToManyTableLazyLoad

Namespace: Spiderly.Shared.Attributes.Entity

Usage:

Specifies that a table items for the many-to-many relationship administration should be loaded lazily (on-demand) rather than eagerly.