Entity

Hello, you can use the writing method in the list entities for the Post only in the Object and try to post in other list entities, some problems in this scenario and migrate as list entities to read-only and the post manually in all , because when using Save or Publish on the first entity in the list (email) it normally writes to the second entity (phone) it returns me or a restriction violation error in the previous table (email), and a restriction was not violated because he managed to save a database on fk. how to proceed in this case?

1 Like

Hello Ronald, can you provide some source code to illustrate your problem?

ExemploProjeto.zip (108.5 KB)

Segue o exemplo do projeto em anexo

Bom dia, alguma posição ?

The error is actually happening in the email table, not telefone. The reason is that when you are saving the telefone, you are sending a JSON containing the user and also an empty email list:

    "$id": 1,
    "@xdata.type": "XData.Default.Telefone",
    "IdTelefone": "00000000-0000-0000-0000-000000000000",
    "CodPais": "+55",
    "Ddd": "",
    "Telefone": "",
    "Identificador": 1,
    "FkUsuario": {
        "$id": 2,
        "@xdata.type": "XData.Default.Usuario",
        "IdUsuario": "811231D7-8BA3-44AE-9AD7-0AA40368D371",
        "Codigo": "1",
        "Classe": 0,
        "NomeRazao": "",
        "Fantasia": "",
        "CpfCnpj": "",
        "RgIe": "",
        "Status": 0,
        "Sexo": "",
        "Senha": "",
        "FkToken": "00000000-0000-0000-0000-000000000000",
        "FkUsuarioAdc": null,
        "ListaIdentificador": [],
        "ListaConexao": [],
        "ListaEmail": [],
        "ListaTelefone": [],
        "ListaEndereco": []
    }
}

Note the arrays ListaIdentificador, ListaConexao, ListaTelefone, ListaEndereco, and, specially ListaEmail.

You are resending the user with an empty list of e-mails. XData and Aurelius will try to update the user, which means update the lists, which means empty the e-mail list, which means removing the e-mail entity from the user entity. For that, it will try to set email.fk_usuario field to null, and then the error will raise.

There are several ways you can fix this, but the easiest and more correct one is simply to set all your lists to lazy. Your lists are in eager mode, that is not recommended in Aurelius.

For example, your current mapping:

    [ManyValuedAssociation([TAssociationProp.Required], [TCascadeType.SaveUpdate, TCascadeType.Merge, TCascadeType.Remove], 'FFkUsuario')]
    FListaEmail: TList<TEmail>;

Should be replaced by:

    [ManyValuedAssociation([TAssociationProp.Lazy, TAssociationProp.Required], CascadeTypeAllRemoveOrphan, 'FFkUsuario')]
    FListaEmail: Proxy<TList<TEmail>>;

And, of course you will have to create a getter for ListaEmail property:

    property ListaEmail: TList<TEmail> read GetListaEmail;
...
function TUsuario.GetListaEmail: TList<TEmail>;
begin
  Result := FListaEmail.Value;
end;

and change the way it's created and destroyed:

constructor TUsuario.Create;
begin
...
  FListaEmail.SetInitialValue(TList<TEmail>.Create);
...
end;

destructor TUsuario.Destroy;
begin
...
  FListaEmail.DestroyValue;
...
  inherited;
end;

Do that for all lists you have in your entities.

1 Like

Deu certo, muito Obrigado !!

1 Like

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.