How to create composite key and ensure the same property in 2 model

Asked

Viewed 101 times

0

I have 3 model

public class UnidadeNegocio
{
    public int Id { get; set; }
    public string Nome { get; set; }
}
public class Cliente
{
    public int Id { get; set; }
    public int UnidadeNegocioId { get; set; }
    public string Nome { get; set;}
}
public class Venda
{
    public int Id { get; set; }
    public int ClienteId { get; set; }
    public int UnidadeNegocioId { get; set; }

    public float Valor{ get; set; }
}

I need to ensure that in the Customer and Sale table, both contain the same Business Unit Id. That is, they must be the same.

In Sql server, we create composite key

How I could create and solve this problem using Fluent Api of Entity Framework?

  • Seeing thus the two tables Customer and Sale, the Customer 12 will always be a Unidadenegocioid = 1, so that play on the Sale table, there is no planning error ai no ?

  • @Virgilionovic this is an example, ID is always dynamic, so there is the table Unidadeneogicio

  • is a bad example, because if you carry the ID together that does not guarantee that I can change in the customer table? I mean, I believe this is a bad example.

  • @Virgilionovic is a visual example only, to have a value "1" as an example... It is not an example of code, but visual...

  • @Virgilionovic edited the question, removed the "example", can help?

  • Marcelo the table are the same. To do this you need a third table that contains a unique ID for Client 1 and Unidadenegocioid = 1 for example and in the Sales table bring this ID generated type Clientunidadenegocio with 3 fields Id, Clienteid and Unidadenegocioid ... maybe I would do so, depends on a context, but at first it would be this.

Show 1 more comment
No answers

Browser other questions tagged

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