Template, relationship with DataAnnotations

1

If I have a Customers entity

public class Cliente
{
    public int ClienteId { get; set; }
    public string Email { get; set; }
    public string Nome { get; set; }
}

If I create a Boleto entity

        public class Boleto
        {
            public int BoletoId { get; set; }
            public string Email { get; set; }
            public int IDC { get; set; }
public virtual Cliente Cliente { get; set; }
       }

If I change the IDC to ClienteId , it understands that this is the relation with the Client, but how to do with DataAnnotation so that there is the relationship with the Client? in the 1-to-1 case

NOTE: I know that I should standardize names, not to turn a salad of names, but in one place I wanted to use another name and then the problem happened.

    
asked by anonymous 03.02.2016 / 16:07

2 answers

2

Just specify the relationship key by following the example below;

public class Boleto
{
    public int BoletoId { get; set; }
    public string Email { get; set; }
    public int IDC { get; set; }
    [ForeignKey("IDC")]
    public virtual Cliente Cliente { get; set; }
}
    
03.02.2016 / 16:32
0

Just remembering that you can indicate the mapping in own FireingnKey , like this:

public class Boleto
{
    public int BoletoId { get; set; }
    public string Email { get; set; }
    [ForeignKey("Cliente")]
    public int IDC { get; set; }

    public virtual Cliente Cliente { get; set; }
}

Or use Fluent API .

    
03.05.2016 / 14:57