Fluent API zero-to-one Relationship 0-1 - bringing results from another relationship

0

I'm trying to make the relationship between my Cliente and CieloToken , tried with Fluent API and Data Annotation , I mixed them both and it did not work.

In fact it does, but even though Cliente does not have CieloToken it ends up bringing some data even if it is 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 that client (3) does not!

    
asked by anonymous 12.05.2016 / 20:46

1 answer

2

Client has a CieloToken or CieloToken have a Client?

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

Model Client:

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; }
}

ClientConfig class (Fluent API):

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

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

        //Configurar outras Propriedades...
    }
}

CieloTokenConfig (Fluent API) Class:

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, place 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.Client.Find (int),

    
13.05.2016 / 01:20