Mapping 1 to 0, 0 to 1

Asked

Viewed 46 times

0

I need to create a map between two existing entities, where both are independent.

So.

I have an entity DepositoTransferencia and the other is Conta. The idea is that at a given time, the entity records DepositoTransferencia are associated only once in Conta, but not at first. But the records of Conta not necessarily related to DepositoTransferencia.

What will happen at the system level is the following: one user will register the deposits/transfers and another user will confirm this release, in which he confirms this release, the system has to provide the amount of deposit/transfer in the account of the user who registered.

What I did:

[Table("DepositosTransferencias")]
public class DepositoTransferencia : BaseEntity
{
    [Key]
    public Guid DepositoTransferenciaId { get; set; }

    [Required]
    [DisplayName("Conta bancária")]
    public int ContaBancoId { get; set; }

    [ForeignKey(nameof(ContaBancoId))]
    public virtual ContaBanco ContaBanco { get; set; }

    [Required]
    [DisplayName("Número")]
    public string NumeroComprovante { get; set; }

    [DisplayName("Número no extrato")]
    public string NumeroExtrato { get; set; }

    [Required]
    [DisplayName("Data")]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime DataDocumento { get; set; }

    [DataType(DataType.Currency)]
    [Required]        
    public decimal Valor { get; set; }

    [DisplayName("Data de liberação")]
    public DateTime? DataLiberacao { get; set; }

    [DisplayName("Observação")]
    public string Observacao { get; set; }

    [Required]
    [DisplayName("Tipo")]
    public DepositoTransferenciaTipo Tipo { get; set; }

    [InverseProperty(nameof(DepositoTransferenciaSituacao.DepositoTransferencia))]
    public virtual ICollection<DepositoTransferenciaSituacao> DepositosTransferenciasSituacoes { get; set; }


}

[Table("Contas")]
public partial class Conta
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ContaId { get; set; }
}

[Table("DepositosTransferenciasContas")]
public class DepositoTransferenciaConta
{
    [Key]
    [ForeignKey(nameof(DepositoTransferencia))]
    [Column(Order = 0)]
    [Index(IsUnique = true)]
    public Guid DepositoTransferenciaId { get; set; }

    [Key]
    [ForeignKey(nameof(Conta))]
    [Column(Order = 1)]
    [Index(IsUnique = true)]
    public int ContaId { get; set; }

    public virtual DepositoTransferencia DepositoTransferencia { get; set; }

    public virtual Conta Conta { get; set; }
}

My question is:

  • If what I did was right.
  • How would I declare the reverse properties in DepositoTransferencia and Conta
  • or what other approach I could use to such an issue.

1 answer

1


You created the entity Depositotransferenciaconta to mediate the link between Depositotransferencia and Bill, however, from what I understood of your rule: "a user will register the deposits/transfers and another user will confirm this release, in which he confirms this release, the system has to provide the amount of deposit/transfer in the account of the user who registered.", I didn’t visualize the need for a connection of many to many between these two entities. I visualize the bank this way:

inserir a descrição da imagem aqui

This way, when registering the deposit the account will already be associated and with the field confirmed you can know if was accepted or not that deposit.

The field confirmed can be exchanged for a field status if you need other status for your deposit, in addition to confirmation.

  • 1

    In reality what I want is a 1 to 1 ratio between Depositotransferencia and Account, why I created the link table, where in the columns ContaId and DepositoTransferenciaId are defined as Unique. However, there may be data in Account that do not exist in Depositotransferencia, as well as data in Depositotransferencia that do not exist in Account. As you propose, I would have a 1-to-N relationship between Account and Depositotransference

  • Whenever you make a deposit it will need to be associated with the account, right? There is a deposit that has no account ?

  • Yes, it will only go to the account after confirmation, if it is not confirmed, it will not exist in the account.

  • Both solutions solve the problem. The difference is that in its solution the payment confirmation is done through another table, in mine, the confirmation is in the deposit entity itself. .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.