Model, relationship with Dataannotations

Asked

Viewed 86 times

1

If I have an entity Customers

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 for ClienteId, he understands that this is the relationship with the Client, but how to do with DataAnnotation so that there is the relationship with the Client? in case 1-to-1

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

2 answers

2


Just specify which 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; }
}
  • specify on public virtual client client? show. .entendi.. thank you

0

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

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.

Browser other questions tagged

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