Fluent API Zero-to-one Relationship 0-1 - Bringing Results From Another Relationship

Asked

Viewed 148 times

0

I’m trying to make the relationship between my Cliente and the CieloToken, tried with Fluent API and Data Annotation, I mixed them together and it didn’t work.

In fact it executes, but even if the Cliente did not have CieloToken he ends up bringing some data even if it’s from another client!

Model:

public class Cliente
{
      [Key]
      [Column("intid")]
      [ForeignKey("ConfigCliente")]
      [Display(Name = "IDC")]
      public int ClienteId { get; set; }

      public virtual ICollection<Boleto> Boletos { get; set; }

      public virtual ConfigCliente ConfigCliente { get; set; }

       public virtual CieloToken CieloToken { get; set; }
}
    //No caso acima quero trazer o CieloToken lembrando que ele não é obrigatório, alguns clientes não tem.

    public class CieloToken
    {
        [Key]
        [Column("int_ID")]
        public int CieloTokenId { get; set; }

        [Column("int_IDC")]
        [Required] //tentativa
        [ForeignKey("Cliente")] //tentativa
        public int ClienteId { get; set; }

        public virtual ICollection<CieloTransacao> cieloTransacao { get; set; }
        public virtual Cliente Cliente { get; set; }
    }
    //Aqui em CieloToken, caso exista sempre terá um Cliente

When I do

var cliente = db.Clientes.Find(3);

It returns a Cielotoken, even if this client(3) does not have!

1 answer

2


Customer has a Cielotoken or Cielotoken has a Customer?

According to what you informed me, I would do the entity clean (no data Annotations) and solve everything in the Fluent API. The code would look something like this:

Model Cliente:

public class Cliente
{
    public int ClienteId { get; set; }
    public virtual ICollection<Boleto> Boletos { get; set; }
    public virtual ConfigCliente ConfigCliente { get; set; }
    public virtual CieloToken CieloToken { get; set; }
}

Model Cielotoken:

public class CieloToken
{
    public int CieloTokenId { get; set; }
    public int ClienteId { get; set; }
    public virtual ICollection<CieloTransacao> cieloTransacao { get; set; }
    public virtual Cliente Cliente { get; set; }
}

Clienteconfig class (Fluent API):

public class ClienteConfig : EntityTypeConfiguration<Cliente>
{
    public ClienteConfig ()
    {
        HasKey(c => c.ClienteId);

        Property(c = c.CieloTokenId)
            .isOptional();

        //Configurar outras Propriedades...
    }
}

Cielotokenconfig class (Fluent API):

public class CieloTokenConfig : EntityTypeConfiguration<CieloToken>
{
    public CieloTokenConfig()
    {
        Haskey(c => c.CielTokenId);

        //Configurar outras Propriedades...

        HasOptional(c => c.Cliente)
            .WithRequired(cl => cl.CieloToken);
    }
}

In your context class, put this method:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
        modelBuilder.Configurations.Add(new ClienteConfig());
        modelBuilder.Configurations.Add(new ClienteTokenConfig());

        base.OnModelCreating(modelBuilder);
}

In your Controller do db.Cliente.Find(int);

  • Cielotoken has a customer, not always a Customer has a Cielotoken.

  • So it seems right to me. Have you tried this code?

Browser other questions tagged

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