Dapper in rationing 1-1 where in class does not have the foreign key

0

When working with ORM it is common (will it be mandatory?) to create the model classes as if they were a mirror of the bank tables. But I have a different case and wanted to know if dapper works this way. In the bank I have the tables. Produto and Fornecedor

Fornecedor:

nome varchar(50)
tel  varchar(11)
cpf  varchar(11) PRIMARY KEY

Produto:

codebar    varchar(13)
nome       varchar(50)
preco      money
custo      money
estoque    int
fornecedor varchar(11) FOREIGN KEY ProdutoToFornecedor(fornecedor) REFERENCES (Fornecedor.cpf)

Note: Ignore SQL, it's only for demonstration purposes.

The Produto class has the attribute of type Fornecedor but does not have an attribute that would represent the foreign key with the provider.

public class Fornecedor
{
    public string Nome {get; set;}
    public string Tel  {get; set;}
    public string cpf  {get; set;}
}

public class Produto
{
    public string     CodeBar   {get; set;}
    public string     Nome      {get; set;}
    public decimal    Preco     {get; set;}
    public decimal    Custo     {get; set;}
    public int        Estoque   {get; set;}
    public Fornecedor Fornec    {get; set;}
}

As can be seen Produto has a property of type Fornecedor but does not have another property that would be the foreign key for the Fornecedor table. I honestly never understood the need for it. If a Produto object has a property that is an object of type Fornecedor which in turn already has the cpf property which is the primary key of the table, because I would still need to have an attribute in Produto what does the foreign key refer to Fornecedor ? That is:

public class Produto
{
    public string     CodeBar   {get; set;}
    public string     Nome      {get; set;}
    public decimal    Preco     {get; set;}
    public decimal    Custo     {get; set;}
    public int        Estoque   {get; set;}
    public string     FornecId  {get; set;}
    public Fornecedor Fornec    {get; set;}
}

This seems redundant to me.

To use ORM, in the case of Dapper is there any way to get away from it? That is, using the Fornec property that is a Fornecedor object and already has the cpf attribute?     

asked by anonymous 23.08.2018 / 22:23

0 answers