<?xml version="1.0" encoding="utf-8"?>
<!-- =====================================================================================
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the repository root for full copyright notice.
========================================================================================== -->
<ECSchema schemaName="ECDbChange" alias="change" version="01.00.01" displayLabel="ECDb Change" xmlns="http://www.bentley.com/schemas/Bentley.ECXML.3.2">
    <ECSchemaReference name="ECDbMap" version="02.00.00" alias="ecdbmap"/>

    <ECEntityClass typeName="ChangeSummary" displayLabel="Summary" modifier="Sealed">
        <ECProperty propertyName="ExtendedProperties" typeName="string" extendedTypeName="Json" />
    </ECEntityClass>

    <ECEnumeration typeName="OpCode" backingTypeName="int" isStrict="true">
        <ECEnumerator name="Insert" value="1" displayLabel="Insert"/>
        <ECEnumerator name="Update" value="2" displayLabel="Update"/>
        <ECEnumerator name="Delete" value="4" displayLabel="Delete"/>
    </ECEnumeration>

    <ECStructClass typeName="InstanceKey" displayLabel="InstanceKey" modifier="Sealed">
        <ECProperty propertyName="Id" typeName="long" extendedTypeName="Id"/>
        <ECProperty propertyName="ClassId" typeName="long" extendedTypeName="Id"/>
    </ECStructClass>
    
    <ECEntityClass typeName="InstanceChange" displayLabel="Instance Change"  modifier="Sealed" description="Represents an instance change in a change summary">
        <ECCustomAttributes>
            <DbIndexList xmlns="ECDbMap.02.00.00">
                <Indexes>
                    <DbIndex>
                        <Name>uix_change_InstanceChange_SummaryId_ChangedInstance</Name>
                        <IsUnique>True</IsUnique>
                        <Properties>
                            <string>Summary.Id</string>
                            <string>ChangedInstance.Id</string>
                            <string>ChangedInstance.ClassId</string>
                        </Properties>
                    </DbIndex>
                </Indexes>
            </DbIndexList>
        </ECCustomAttributes>
        <ECNavigationProperty propertyName="Summary" relationshipName="ChangeSummaryContainsInstanceChanges" direction="backward" >
            <ECCustomAttributes>
                <ForeignKeyConstraint xmlns="ECDbMap.02.00.00">
                    <OnDeleteAction>Cascade</OnDeleteAction>
                </ForeignKeyConstraint>
            </ECCustomAttributes>
        </ECNavigationProperty> 
        <ECStructProperty propertyName="ChangedInstance" typeName="InstanceKey" description="Key of the change instance"/>
        <ECProperty propertyName="OpCode" typeName="OpCode"/>
        <ECProperty propertyName="IsIndirect" typeName="bool" description="Change happened due to a foreign key action or trigger"/>
    </ECEntityClass>
    
    <ECEntityClass typeName="PropertyValueChange" displayLabel="Property Value Change" modifier="Sealed" description="Represents an property value change of an instance change in a change summary">
        <ECCustomAttributes>
            <DbIndexList xmlns="ECDbMap.02.00.00">
                <Indexes>
                    <DbIndex>
                        <Name>uix_change_PropertyValueChange_InstanceChangeId_AccessString</Name>
                        <IsUnique>True</IsUnique>
                        <Properties>
                            <string>InstanceChange.Id</string>
                            <string>AccessString</string>
                        </Properties>
                    </DbIndex>		
                </Indexes>
            </DbIndexList>            
        </ECCustomAttributes>
        <ECNavigationProperty propertyName="InstanceChange" relationshipName="InstanceChangeOwnsPropertyValueChanges" direction="backward">
            <ECCustomAttributes>
                <ForeignKeyConstraint xmlns="ECDbMap.02.00.00">
                    <OnDeleteAction>Cascade</OnDeleteAction>
                </ForeignKeyConstraint>
            </ECCustomAttributes>
        </ECNavigationProperty>
        <ECProperty propertyName="AccessString" typeName="string"/>
        <ECProperty propertyName="RawOldValue" typeName="binary" description="Untyped old value"/>
        <ECProperty propertyName="RawNewValue" typeName="binary" description="Untyped new value"/>
    </ECEntityClass>
    
    <ECRelationshipClass typeName="ChangeSummaryContainsInstanceChanges" modifier="Sealed" strength="embedding">
        <Source multiplicity="(1..1)" roleLabel="has" polymorphic="false">
            <Class class="ChangeSummary"/>
        </Source>
        <Target multiplicity="(0..*)" roleLabel="is contained in" polymorphic="false">
            <Class class="InstanceChange"/>
        </Target>
    </ECRelationshipClass>
    
    <ECRelationshipClass typeName="InstanceChangeOwnsPropertyValueChanges" modifier="Sealed" strength="embedding">
        <Source multiplicity="(1..1)" roleLabel="owns" polymorphic="false">
            <Class class="InstanceChange"/>
        </Source>
        <Target multiplicity="(0..*)" roleLabel="is contained in" polymorphic="false">
            <Class class="PropertyValueChange"/>
        </Target>
    </ECRelationshipClass>
</ECSchema>
