Skip to content

code generation conventions

samatstarion edited this page Jun 3, 2025 · 25 revisions

Reference properties

In order to translate a UML model to code we make use of the following conventions for reference properties, i.e. Associations and Composite Aggregation. The assumption we make is that each class has a property that is it's unique identifier, the unique identifier is used as reference pointer for DTO and SQL foreign key constraint generation. We typically make use of Guid or UUID for the unique identifier.

ID Reference Property
A001 A [0..1] --> [0..1] B
A002 A [0..1] --> [1..1] B
A003 A [0..1] --> [0..*] B
A004 A [0..1] --> [1..*] B
A005 A [0..1] --> [n..*] B
A006 A [1..1] --> [0..1] B
A007 A [1..1] --> [1..1] B
A008 A [1..1] --> [0..*] B
A009 A [1..1] --> [1..*] B
A010 A [1..1] --> [n..*] B
A011 A [0..*] --> [0..1] B
A012 A [0..*] --> [1..1] B
A013 A [0..] --> [0..] B
A014 A [0..] --> [1..] B
A015 A [0..] --> [n..] B
A016 A [1..*] --> [0..1] B
A017 A [1..*] --> [1..1] B
A018 A [1..] --> [0..] B
A019 A [1..] --> [1..] B
A020 A [1..] --> [n..] B
A021 A [n..*] --> [0..1] B
A022 A [n..*] --> [1..1] B
A023 A [n..] --> [0..] B
A024 A [n..] --> [1..] B
A025 A [n..] --> [n..] B
A026 A [1..1] ◆--> [0..1] B
A027 A [1..1] ◆--> [1..1] B
A028 A [1..1] ◆--> [0..*] B
A029 A [1..1] ◆--> [1..*] B
A020 A [1..1] ◆--> [n..*] B

Associations

A001

A [0..1] --> [0..1] B

   _ _ _ _                      _ _ _ _
  |       | [0..1]     [0..1]  |       |
  |   A   |------------------->|   B   |
  |_ _ _ _| prop_a     prop_b  |_ _ _ _|

DTO:  A.prop_b
POCO: A.prop_b
SQL:  A.prop_b

DTO

public class A {

    /// <summary>
    /// Gets or sets the (nullable) unique identifier of <see cref="B"/>
    /// </summary>
    public Guid? Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

POCO

public class A {

    /// <summary>
    /// Gets or sets the (nullable) reference to <see cref="B"/>
    /// </summary>
    public B? Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

SQL

CREATE TABLE "A" (
    id UUID PRIMARY KEY,
    b_id UUID
)

CREATE TABLE "B" (
    id UUID PRIMARY KEY
)

ALTER TABLE "A" ADD CONSTRAINT fk_b FOREIGN KEY (b_id) REFERENCES "B"("id") DEFERRABLE;

A002

A [0..1] --> [1..1] B

   _ _ _ _                      _ _ _ _ 
  |       | [0..1]     [1..1]  |       |
  |   A   |------------------->|   B   |
  |_ _ _ _| prop_a     prop_b  |_ _ _ _|

DTO:  A.prop_b
POCO: A.prop_b
SQL:  A.prop_b -> Because of possibility of referential integrity check

DTO

public class A {

    /// <summary>
    /// Gets or sets the unique identifier of <see cref="B"/>
    /// </summary>
    public Guid Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

POCO

public class A {

    /// <summary>
    /// Gets or sets the reference to <see cref="B"/>
    /// </summary>
    public B Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

SQL

CREATE TABLE "A" (
    id UUID PRIMARY KEY
    b_id UUID NOT NULL
)

CREATE TABLE "B" (
    id UUID PRIMARY KEY,
)

ALTER TABLE "A" ADD CONSTRAINT fk_b FOREIGN KEY (b_id) REFERENCES "B"("id") DEFERRABLE;

A003

A [0..1] --> [0..*] B

   _ _ _ _                (>1)  _ _ _ _ 
  |       | [0..1]     [0..*]  |       |
  |   A   |------------------->|   B   |
  |_ _ _ _| prop_a     prop_b  |_ _ _ _|

DTO:  A.prop_b
POCO: A.prop_b
SQL:  B.prop_a

DTO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public List<Guid> Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

POCO

public class A {

    /// <summary>
    /// Gets or sets the list of reference to <see cref="B"/>
    /// </summary>
    public List<B> Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

SQL

CREATE TABLE "A" (
    id UUID PRIMARY KEY
)

CREATE TABLE "B" (
    id UUID PRIMARY KEY,
    a_id UUID
)

ALTER TABLE "B" ADD CONSTRAINT fk_a FOREIGN KEY (a_id) REFERENCES "A"("id") DEFERRABLE;

A004

A [0..1] --> [1..*] B

   _ _ _ _                (>1)  _ _ _ _ 
  |       | [0..1]     [1..*]  |       |
  |   A   |---------- -------->|   B   |
  |_ _ _ _| prop_a     prop_b  |_ _ _ _|

DTO:  A.prop_b
POCO: A.prop_b
SQL:  B.prop_a

DTO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public List<Guid> Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

POCO

public class A {

    /// <summary>
    /// Gets or sets the list of reference to <see cref="B"/>
    /// </summary>
    public List<B> Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

SQL

CREATE TABLE "A" (
    id UUID PRIMARY KEY
)

CREATE TABLE "B" (
    id UUID PRIMARY KEY,
    a_id UUID
)

ALTER TABLE "B" ADD CONSTRAINT fk_a FOREIGN KEY (a_id) REFERENCES "A"("id") DEFERRABLE;

A005

A [0..1] --> [n..*] B

   _ _ _ _            (>1)(>1)  _ _ _ _ 
  |       | [0..1]     [n..*]  |       |
  |   A   |---------- -------->|   B   |
  |_ _ _ _| prop_a     prop_b  |_ _ _ _|

DTO:  A.prop_b
POCO: A.prop_b
SQL:  B.prop_a

DTO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public List<Guid> Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

POCO

public class A {

    /// <summary>
    /// Gets or sets the list of reference to <see cref="B"/>
    /// </summary>
    public List<B> Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

SQL

CREATE TABLE "A" (
    id UUID PRIMARY KEY
)

CREATE TABLE "B" (
    id UUID PRIMARY KEY,
    a_id UUID
)

ALTER TABLE "B" ADD CONSTRAINT fk_a FOREIGN KEY (a_id) REFERENCES "A"("id") DEFERRABLE;

A006

A [1..1] --> [0..1] B

   _ _ _ _                      _ _ _ _ 
  |       | [1..1]     [0..1]  |       |
  |   A   |------------------->|   B   |
  |_ _ _ _| prop_a     prop_b  |_ _ _ _|

DTO:  A.prop_b
POCO: A.prop_b
SQL:  B.prop_a

DTO

public class A {

    /// <summary>
    /// Gets or sets the (nullable) unique identifier of <see cref="B"/>
    /// </summary>
    public Guid? Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

POCO

public class A {

    /// <summary>
    /// Gets or sets the list of reference to <see cref="B"/>
    /// </summary>
    public B? Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

SQL

CREATE TABLE "A" (
    id UUID PRIMARY KEY
)

CREATE TABLE "B" (
    id UUID PRIMARY KEY,
    a_id UUID NOT NULL
)

ALTER TABLE "B" ADD CONSTRAINT fk_a FOREIGN KEY (a_id) REFERENCES "A"("id") DEFERRABLE;

A007

A [1..1] --> [1..1] B

   _ _ _ _                      _ _ _ _ 
  |       | [1..1]     [1..1]  |       |
  |   A   |------------------->|   B   |
  |_ _ _ _| prop_a     prop_b  |_ _ _ _|

DTO:  A.prop_b
POCO: A.prop_b
SQL:  B.prop_a

DTO

public class A {

    /// <summary>
    /// Gets or sets the unique identifier of <see cref="B"/>
    /// </summary>
    public Guid Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

POCO

public class A {

    /// <summary>
    /// Gets or sets the unique identifier of <see cref="B"/>
    /// </summary>
    public B Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

SQL

CREATE TABLE "A" (
    id UUID PRIMARY KEY
)

CREATE TABLE "B" (
    id UUID PRIMARY KEY,
    a_id UUID NOT NULL
)

ALTER TABLE "B" ADD CONSTRAINT fk_a FOREIGN KEY (a_id) REFERENCES "A"("id") DEFERRABLE;

A008

A [1..1] --> [0..*] B

   _ _ _ _                (>1)  _ _ _ _ 
  |       | [1..1]     [0..*]  |       |
  |   A   |------------------->|   B   |
  |_ _ _ _| prop_a     prop_b  |_ _ _ _|

DTO:  A.prop_b
POCO: A.prop_b
SQL:  B.prop_a

DTO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public List<Guid> Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

POCO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public List<B> Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

SQL

CREATE TABLE "A" (
    id UUID PRIMARY KEY
)

CREATE TABLE "B" (
    id UUID PRIMARY KEY,
    a_id UUID NOT NULL
)

ALTER TABLE "B" ADD CONSTRAINT fk_a FOREIGN KEY (a_id) REFERENCES "A"("id") DEFERRABLE;

A009

A [1..1] --> [1..*] B

   _ _ _ _                (>1)  _ _ _ _ 
  |       | [1..1]     [1..*]  |       |
  |   A   |---------- -------->|   B   |
  |_ _ _ _| prop_a     prop_b  |_ _ _ _|

DTO:  A.prop_b
POCO: A.prop_b
SQL:  B.prop_a

DTO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public List<Guid> Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

POCO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public List<Guid> Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

SQL

CREATE TABLE "A" (
    id UUID PRIMARY KEY
)

CREATE TABLE "B" (
    id UUID PRIMARY KEY,
    a_id UUID NOT NULL
)

ALTER TABLE "B" ADD CONSTRAINT fk_a FOREIGN KEY (a_id) REFERENCES "A"("id") DEFERRABLE;

A010

A [1..1] --> [n..*] B

   _ _ _ _            (>1)(>1)  _ _ _ _ 
  |       | [1..1]     [n..*]  |       |
  |   A   |---------- -------->|   B   |
  |_ _ _ _| prop_a     prop_b  |_ _ _ _|

DTO:  A.prop_b
POCO: A.prop_b
SQL:  B.prop_a

DTO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public List<Guid> Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

POCO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public List<B> Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

SQL

CREATE TABLE "A" (
    id UUID PRIMARY KEY
)

CREATE TABLE "B" (
    id UUID PRIMARY KEY,
    a_id UUID NOT NULL
)

ALTER TABLE "B" ADD CONSTRAINT fk_a FOREIGN KEY (a_id) REFERENCES "A"("id") DEFERRABLE;

A011

A [0..*] --> [0..1] B

   _ _ _ _                      _ _ _ _ 
  |       | [0..*]     [0..1]  |       |
  |   A   |------------------->|   B   |
  |_ _ _ _| prop_a     prop_b  |_ _ _ _|

DTO:  A.prop_b
POCO: A.prop_b
SQL:  A.prop_b -> Because of cardinality switch

DTO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public Guid? Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

POCO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public B? Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

SQL

CREATE TABLE "A" (
    id UUID PRIMARY KEY,
    b_id UUID
)

CREATE TABLE "B" (
    id UUID PRIMARY KEY
)

ALTER TABLE "A" ADD CONSTRAINT fk_b FOREIGN KEY (b_id) REFERENCES "B"("id") DEFERRABLE;

A012

A [0..*] --> [1..1] B

   _ _ _ _                      _ _ _ _ 
  |       | [0..*]     [1..1]  |       |
  |   A   |------------------->|   B   |
  |_ _ _ _| prop_a     prop_b  |_ _ _ _|

DTO:  A.prop_b
POCO: A.prop_b
SQL:  A.prop_b -> Because of cardinality switch

DTO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public Guid Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

POCO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public B Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

SQL

CREATE TABLE "A" (
    id UUID PRIMARY KEY,
    b_id UUID NOT NULL
)

CREATE TABLE "B" (
    id UUID PRIMARY KEY
)

ALTER TABLE "A" ADD CONSTRAINT fk_b FOREIGN KEY (b_id) REFERENCES "B"("id") DEFERRABLE;

A013

A [0..*] --> [0..*] B

   _ _ _ _                (>1)  _ _ _ _ 
  |       | [0..*]     [0..*]  |       |
  |   A   |------------------->|   B   |
  |_ _ _ _| prop_a     prop_b  |_ _ _ _|

DTO:  A.prop_b
POCO: A.prop_b
SQL:  A.prop_a__B -> Junction Table

DTO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public List<Guid> Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

POCO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public List<Guid> Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

SQL

CREATE TABLE "A" (
    id UUID PRIMARY KEY
)

CREATE TABLE "B" (
    id UUID PRIMARY KEY
)

CREATE TABLE "A_b_id__B"
(
    "sourceA" UUID NOT NULL,
    "targetB" UUID NOT NULL,
    PRIMARY KEY ("sourceA", "targetB")
)

ALTER TABLE "A_b_id__B" ADD CONSTRAINT "A_FK_Source" FOREIGN KEY ("sourceA") REFERENCES "A" ("id") ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;
ALTER TABLE "A_b_id__B" ADD CONSTRAINT "B_FK_Target" FOREIGN KEY ("targetB") REFERENCES "B" ("id") ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;

A014

A [0..*] --> [1..*] B

   _ _ _ _                (>1)  _ _ _ _ 
  |       | [0..*]     [1..*]  |       |
  |   A   |---------- -------->|   B   |
  |_ _ _ _| prop_a     prop_b  |_ _ _ _|

DTO:  A.prop_b
POCO: A.prop_b
SQL:  A.prop_a__B -> Junction Table

DTO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public List<Guid> Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

POCO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public List<B> Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

SQL

CREATE TABLE "A" (
    id UUID PRIMARY KEY
)

CREATE TABLE "B" (
    id UUID PRIMARY KEY
)

CREATE TABLE "A_b_id__B"
(
    "sourceA" UUID NOT NULL,
    "targetB" UUID NOT NULL,
    PRIMARY KEY ("sourceA", "targetB")
)

ALTER TABLE "A_b_id__B" ADD CONSTRAINT "A_FK_Source" FOREIGN KEY ("sourceA") REFERENCES "A" ("id") ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;
ALTER TABLE "A_b_id__B" ADD CONSTRAINT "B_FK_Target" FOREIGN KEY ("targetB") REFERENCES "B" ("id") ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;

A015

A [0..*] --> [n..*] B

   _ _ _ _            (>1)(>1)  _ _ _ _ 
  |       | [0..*]     [n..*]  |       |
  |   A   |---------- -------->|   B   |
  |_ _ _ _| prop_a     prop_b  |_ _ _ _|

DTO:  A.prop_b
POCO: A.prop_b
SQL:  A.prop_a__B -> Junction Table

DTO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public List<Guid> Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

POCO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public List<B> Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

SQL

CREATE TABLE "A" (
    id UUID PRIMARY KEY
)

CREATE TABLE "B" (
    id UUID PRIMARY KEY
)

CREATE TABLE "A_b_id__B"
(
    "sourceA" UUID NOT NULL,
    "targetB" UUID NOT NULL,
    PRIMARY KEY ("sourceA", "targetB")
)

ALTER TABLE "A_b_id__B" ADD CONSTRAINT "A_FK_Source" FOREIGN KEY ("sourceA") REFERENCES "A" ("id") ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;
ALTER TABLE "A_b_id__B" ADD CONSTRAINT "B_FK_Target" FOREIGN KEY ("targetB") REFERENCES "B" ("id") ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;

A016

A [1..*] --> [0..1] B

   _ _ _ _                      _ _ _ _ 
  |       | [1..*]     [0..1]  |       |
  |   A   |------------------->|   B   |
  |_ _ _ _| prop_a     prop_b  |_ _ _ _|

DTO:  A.prop_b
POCO: A.prop_b
SQL:  B.prop_a

DTO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public Guid? Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

POCO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public B? Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

SQL

CREATE TABLE "A" (
    id UUID PRIMARY KEY,
    b_id UUID
)

CREATE TABLE "B" (
    id UUID PRIMARY KEY
)

ALTER TABLE "A" ADD CONSTRAINT fk_b FOREIGN KEY (b_id) REFERENCES "B"("id") DEFERRABLE;

A017

A [1..*] --> [1..1] B

   _ _ _ _                      _ _ _ _ 
  |       | [1..*]     [1..1]  |       |
  |   A   |------------------->|   B   |
  |_ _ _ _| prop_a     prop_b  |_ _ _ _|

DTO:  A.prop_b
POCO: A.prop_b
SQL:  B.prop_a

DTO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public Guid Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

POCO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public B Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

SQL

CREATE TABLE "A" (
    id UUID PRIMARY KEY,
    b_id UUID NOT NULL
)

CREATE TABLE "B" (
    id UUID PRIMARY KEY
)

ALTER TABLE "A" ADD CONSTRAINT fk_b FOREIGN KEY (b_id) REFERENCES "B"("id") DEFERRABLE;

A018

A [1..*] --> [0..*] B

   _ _ _ _                (>1)  _ _ _ _ 
  |       | [1..*]     [0..*]  |       |
  |   A   |------------------->|   B   |
  |_ _ _ _| prop_a     prop_b  |_ _ _ _|

DTO:  A.prop_b
POCO: A.prop_b
SQL:  A_prop_a__B -> Many to Many table

DTO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public List<Guid> Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

POCO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public List<B> Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

SQL

CREATE TABLE "A" (
    id UUID PRIMARY KEY
)

CREATE TABLE "B" (
    id UUID PRIMARY KEY
)

CREATE TABLE "A_b_id__B"
(
    "sourceA" UUID NOT NULL,
    "targetB" UUID NOT NULL,
    PRIMARY KEY ("sourceA", "targetB")
)

ALTER TABLE "A_b_id__B" ADD CONSTRAINT "A_FK_Source" FOREIGN KEY ("sourceA") REFERENCES "A" ("id") ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;
ALTER TABLE "A_b_id__B" ADD CONSTRAINT "B_FK_Target" FOREIGN KEY ("targetB") REFERENCES "B" ("id") ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;

A019

A [1..*] --> [1..*] B

   _ _ _ _                (>1)  _ _ _ _ 
  |       | [1..*]     [1..*]  |       |
  |   A   |---------- -------->|   B   |
  |_ _ _ _| prop_a     prop_b  |_ _ _ _|

DTO:  A.prop_b
POCO: A.prop_b
SQL:  A_prop_a__B -> Many to Many table

DTO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public List<Guid> Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

POCO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public List<B> Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

SQL

CREATE TABLE "A" (
    id UUID PRIMARY KEY
)

CREATE TABLE "B" (
    id UUID PRIMARY KEY
)

CREATE TABLE "A_b_id__B"
(
    "sourceA" UUID NOT NULL,
    "targetB" UUID NOT NULL,
    PRIMARY KEY ("sourceA", "targetB")
)

ALTER TABLE "A_b_id__B" ADD CONSTRAINT "A_FK_Source" FOREIGN KEY ("sourceA") REFERENCES "A" ("id") ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;
ALTER TABLE "A_b_id__B" ADD CONSTRAINT "B_FK_Target" FOREIGN KEY ("targetB") REFERENCES "B" ("id") ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;

A020

A [1..*] --> [n..*] B

   _ _ _ _            (>1)(>1)  _ _ _ _ 
  |       | [1..*]     [n..*]  |       |
  |   A   |------------------->|   B   |
  |_ _ _ _| prop_a     prop_b  |_ _ _ _|

DTO:  A.prop_b
POCO: A.prop_b
SQL:  A_prop_a__B -> Many to Many table

DTO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public List<Guid> Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

POCO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public List<B> Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

SQL

CREATE TABLE "A" (
    id UUID PRIMARY KEY
)

CREATE TABLE "B" (
    id UUID PRIMARY KEY
)

CREATE TABLE "A_b_id__B"
(
    "sourceA" UUID NOT NULL,
    "targetB" UUID NOT NULL,
    PRIMARY KEY ("sourceA", "targetB")
)

ALTER TABLE "A_b_id__B" ADD CONSTRAINT "A_FK_Source" FOREIGN KEY ("sourceA") REFERENCES "A" ("id") ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;
ALTER TABLE "A_b_id__B" ADD CONSTRAINT "B_FK_Target" FOREIGN KEY ("targetB") REFERENCES "B" ("id") ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;

A021

A [n..*] --> [0..1] B

   _ _ _ _                      _ _ _ _ 
  |       | [n..*]     [0..1]  |       |
  |   A   |------------------->|   B   |
  |_ _ _ _| prop_a     prop_b  |_ _ _ _|

DTO:  A.prop_b
POCO: A.prop_b
SQL:  A_prop_b -> because of single reference value on A side

DTO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public Guid? Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

POCO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public B? Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

SQL

CREATE TABLE "A" (
    id UUID PRIMARY KEY,
    b_id UUID
)

CREATE TABLE "B" (
    id UUID PRIMARY KEY
)

ALTER TABLE "A" ADD CONSTRAINT fk_b FOREIGN KEY (b_id) REFERENCES "B"("id") DEFERRABLE;

A022

A [n..*] --> [1..1] B

   _ _ _ _                      _ _ _ _ 
  |       | [n..*]     [1..1]  |       |
  |   A   |------------------->|   B   |
  |_ _ _ _| prop_a     prop_b  |_ _ _ _|

DTO:  A.prop_b
POCO: A.prop_b
SQL:  A_prop_b -> because of single reference value on A side

DTO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public Guid Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

POCO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public B Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

SQL

CREATE TABLE "A" (
    id UUID PRIMARY KEY,
    b_id UUID NOT NULL
)

CREATE TABLE "B" (
    id UUID PRIMARY KEY
)

ALTER TABLE "A" ADD CONSTRAINT fk_b FOREIGN KEY (b_id) REFERENCES "B"("id") DEFERRABLE;

A023

A [n..*] --> [0..*] B

   _ _ _ _                (>1)  _ _ _ _ 
  |       | [n..*]     [0..*]  |       |
  |   A   |------------------->|   B   |
  |_ _ _ _| prop_a     prop_b  |_ _ _ _|

DTO:  A.prop_b
POCO: A.prop_b
SQL:  A_prop_a__B -> Junction Table

DTO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public List<Guid> Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

POCO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public List<B> Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

SQL

CREATE TABLE "A" (
    id UUID PRIMARY KEY
)

CREATE TABLE "B" (
    id UUID PRIMARY KEY
)

CREATE TABLE "A_b_id__B"
(
    "sourceA" UUID NOT NULL,
    "targetB" UUID NOT NULL,
    PRIMARY KEY ("sourceA", "targetB")
)

ALTER TABLE "A_b_id__B" ADD CONSTRAINT "A_FK_Source" FOREIGN KEY ("sourceA") REFERENCES "A" ("id") ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;
ALTER TABLE "A_b_id__B" ADD CONSTRAINT "B_FK_Target" FOREIGN KEY ("targetB") REFERENCES "B" ("id") ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;

A024

A [n..*] --> [1..*] B

   _ _ _ _                (>1)  _ _ _ _ 
  |       | [n..*]     [1..*]  |       |
  |   A   |---------- -------->|   B   |
  |_ _ _ _| prop_a     prop_b  |_ _ _ _|

DTO:  A.prop_b
POCO: A.prop_b
SQL:  A_prop_a__B -> Junction Table

DTO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public List<Guid> Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

POCO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public List<B> Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

SQL

CREATE TABLE "A" (
    id UUID PRIMARY KEY
)

CREATE TABLE "B" (
    id UUID PRIMARY KEY
)

CREATE TABLE "A_b_id__B"
(
    "sourceA" UUID NOT NULL,
    "targetB" UUID NOT NULL,
    PRIMARY KEY ("sourceA", "targetB")
)

ALTER TABLE "A_b_id__B" ADD CONSTRAINT "A_FK_Source" FOREIGN KEY ("sourceA") REFERENCES "A" ("id") ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;
ALTER TABLE "A_b_id__B" ADD CONSTRAINT "B_FK_Target" FOREIGN KEY ("targetB") REFERENCES "B" ("id") ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;

A025

A [n..*] --> [n..*] B

   _ _ _ _            (>1)(>1)  _ _ _ _ 
  |       | [n..*]     [n..*]  |       |
  |   A   |---------- -------->|   B   |
  |_ _ _ _| prop_a     prop_b  |_ _ _ _|

DTO:  A.prop_b
POCO: A.prop_b
SQL:  A_prop_a__B -> Junction Table

DTO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public List<Guid> Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

POCO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// </summary>
    public List<B> Prop_b { get; set; }
}

public class B {
    // B does not know of A
}

SQL

CREATE TABLE "A" (
    id UUID PRIMARY KEY
)

CREATE TABLE "B" (
    id UUID PRIMARY KEY
)

CREATE TABLE "A_b_id__B"
(
    "sourceA" UUID NOT NULL,
    "targetB" UUID NOT NULL,
    PRIMARY KEY ("sourceA", "targetB")
)

ALTER TABLE "A_b_id__B" ADD CONSTRAINT "A_FK_Source" FOREIGN KEY ("sourceA") REFERENCES "A" ("id") ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;
ALTER TABLE "A_b_id__B" ADD CONSTRAINT "B_FK_Target" FOREIGN KEY ("targetB") REFERENCES "B" ("id") ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;

Composite Aggregration

Important

Please note that for composite aggregation on the owner side we only support [1..1] as other multiplicities are not supported in our modelling strategy and code generation.

We typicaly make use of the ContainerList<T>, this is a convenience collection that sets the owner or container object.

A026

A [1..1] ◆--> [0..1] B

   _ _ _ _                      _ _ _ _ 
  |       | [1..1]     [0..1]  |       |
  |   A   |◆------------------>|   B   |
  |_ _ _ _| prop_a     prop_b  |_ _ _ _|

DTO:  A.prop_b
      B.Container
POCO: A.prop_b
      B.Container
SQL:  B.prop_a

DTO

public class A {

    /// <summary>
    /// Gets or sets the unique identifier of <see cref="B"/>
    /// that may be contained by <see cref="A"/>
    /// </summary>
    public List<Guid> Prop_b { get; set; } = [];
}

public class B {

    /// <summary>
    /// The Container <see cref="A"/>
    /// </summary>
    public Guid Container { get; set; }
}

POCO

public class A {

    /// <summary>
    /// Initializes a new instance of the <see cref="A"/> class.
    /// </summary>
    public A()
    {
        this.Prop_b = new ContainerList<B>(this);
    }

    /// <summary>
    /// Gets or sets the instance of <see cref="B"/>
    /// that may be contained by <see cref="A"/>
    /// </summary>
    public ContainerList<B> Prop_b { get; set; };
}

public class B {

    /// <summary>
    /// The Container <see cref="A"/>
    /// </summary>
    public A Container { get; set; }
}

SQL

CREATE TABLE "A" (
    id UUID PRIMARY KEY
)

CREATE TABLE "B" (
    id UUID PRIMARY KEY,
    a_id UUID NOT NULL
)

ALTER TABLE "B" ADD CONSTRAINT fk_a FOREIGN KEY (a_id) REFERENCES "A"("id") ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;

A027

A [1..1] ◆--> [1..1] B

   _ _ _ _                      _ _ _ _ 
  |       | [1..1]     [1..1]  |       |
  |   A   |◆------------------>|   B   |
  |_ _ _ _| prop_a     prop_b  |_ _ _ _|

DTO:  A.prop_b
      B.Container
POCO: A.prop_b
      B.Container
SQL:  B.prop_a

DTO

public class A {

    /// <summary>
    /// Gets or sets the unique identifier of <see cref="B"/>
    /// that may be contained by <see cref="A"/>
    /// </summary>
    public List<Guid> Prop_b { get; set; } = [];
}

public class B {

    /// <summary>
    /// The Container <see cref="A"/>
    /// </summary>
    public Guid Container { get; set; }
}

POCO

public class A {

    /// <summary>
    /// Initializes a new instance of the <see cref="A"/> class.
    /// </summary>
    public A()
    {
        this.Prop_b = new ContainerList<B>(this);
    }

    /// <summary>
    /// Gets or sets the instance of <see cref="B"/>
    /// that may be contained by <see cref="A"/>
    /// </summary>
    public ContainerList<B> Prop_b { get; set; };
}

public class B {

    /// <summary>
    /// The Container <see cref="A"/>
    /// </summary>
    public A Container { get; set; }
}

SQL

CREATE TABLE "A" (
    id UUID PRIMARY KEY
)

CREATE TABLE "B" (
    id UUID PRIMARY KEY,
    a_id UUID NOT NULL
)

ALTER TABLE "B" ADD CONSTRAINT fk_a FOREIGN KEY (a_id) REFERENCES "A"("id") ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;

A028

A [1..1] ◆--> [0..*] B

   _ _ _ _                (>1)  _ _ _ _  
  |       | [1..1]     [0..*]   |       |
  |   A   |◆------------------->|   B   |
  |_ _ _ _| prop_a     prop_b   |_ _ _ _|

DTO:  A.prop_b
      B.Container
POCO: A.prop_b
      B.Container
SQL:  B.prop_a

DTO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// that are contained by <see cref="A"/>
    /// </summary>
    public List<Guid> Prop_b { get; set; } = [];
}

public class B {

    /// <summary>
    /// The Container <see cref="A"/>
    /// </summary>
    public Guid Container { get; set; }
}

POCO

public class A {

    /// <summary>
    /// Initializes a new instance of the <see cref="A"/> class.
    /// </summary>
    public A()
    {
        this.Prop_b = new ContainerList<B>(this);
    }

    /// <summary>
    /// Gets or sets the instance of <see cref="B"/>
    /// that may be contained by <see cref="A"/>
    /// </summary>
    public ContainerList<B> Prop_b { get; set; };
}

public class B {

    /// <summary>
    /// The Container <see cref="A"/>
    /// </summary>
    public A Container { get; set; }
}

SQL

CREATE TABLE "A" (
    id UUID PRIMARY KEY
)

CREATE TABLE "B" (
    id UUID PRIMARY KEY,
    a_id UUID NOT NULL
)

ALTER TABLE "B" ADD CONSTRAINT fk_a FOREIGN KEY (a_id) REFERENCES "A"("id") ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;

A029

A [1..1] ◆--> [1..*] B

   _ _ _ _                (>1)  _ _ _ _ 
  |       | [1..1]     [1..*]  |       |
  |   A   |◆--------- -------->|   B   |
  |_ _ _ _| prop_a     prop_b  |_ _ _ _|

DTO:  A.prop_b
      B.Container
POCO: A.prop_b
      B.Container
SQL:  B.prop_a

DTO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// that are contained by <see cref="A"/>
    /// </summary>
    public List<Guid> Prop_b { get; set; } = [];
}

public class B {

    /// <summary>
    /// The Container <see cref="A"/>
    /// </summary>
    public Guid Container { get; set; }
}

POCO

public class A {

    /// <summary>
    /// Initializes a new instance of the <see cref="A"/> class.
    /// </summary>
    public A()
    {
        this.Prop_b = new ContainerList<B>(this);
    }

    /// <summary>
    /// Gets or sets the instance of <see cref="B"/>
    /// that may be contained by <see cref="A"/>
    /// </summary>
    public ContainerList<B> Prop_b { get; set; };
}

public class B {

    /// <summary>
    /// The Container <see cref="A"/>
    /// </summary>
    public A Container { get; set; }
}

SQL

CREATE TABLE "A" (
    id UUID PRIMARY KEY
)

CREATE TABLE "B" (
    id UUID PRIMARY KEY,
    a_id UUID NOT NULL
)

ALTER TABLE "B" ADD CONSTRAINT fk_a FOREIGN KEY (a_id) REFERENCES "A"("id") ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;

A020

A [1..1] ◆--> [n..*] B

   _ _ _ _            (>1)(>1)  _ _ _ _ 
  |       | [1..1]     [n..*]  |       |
  |   A   |◆--------- -------->|   B   |
  |_ _ _ _| prop_a     prop_b  |_ _ _ _|

DTO:  A.prop_b
      B.Container
POCO: A.prop_b
      B.Container
SQL:  B.prop_a

DTO

public class A {

    /// <summary>
    /// Gets or sets the unique identifiers of <see cref="B"/>
    /// that are contained by <see cref="A"/>
    /// </summary>
    public List<Guid> Prop_b { get; set; } = [];
}

public class B {

    /// <summary>
    /// The Container <see cref="A"/>
    /// </summary>
    public Guid Container { get; set; }
}

POCO

public class A {

    /// <summary>
    /// Initializes a new instance of the <see cref="A"/> class.
    /// </summary>
    public A()
    {
        this.Prop_b = new ContainerList<B>(this);
    }

    /// <summary>
    /// Gets or sets the instance of <see cref="B"/>
    /// that may be contained by <see cref="A"/>
    /// </summary>
    public ContainerList<B> Prop_b { get; set; };
}

public class B {

    /// <summary>
    /// The Container <see cref="A"/>
    /// </summary>
    public A Container { get; set; }
}

SQL

CREATE TABLE "A" (
    id UUID PRIMARY KEY
)

CREATE TABLE "B" (
    id UUID PRIMARY KEY,
    a_id UUID NOT NULL
)

ALTER TABLE "B" ADD CONSTRAINT fk_a FOREIGN KEY (a_id) REFERENCES "A"("id") ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;
Clone this wiki locally