Doubt with relationships N to N or 1 to N

Asked

Viewed 46 times

-2

Given the following scenario. I have 1 user table and a login table, for example. I need to create my domain class that maps these BD entities. In the login table, I get Idusuario. How it looks in my model this relationship?

[Table("Usuario")]
    public class Usuario
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int IdUsuario { get; set; }
        public string NMUsuario { get; set; }
        public string Senha { get; set; }
    }

[Table("Login")]
    public class Login
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int IdLogin { get; set; }
        public DateTime DtLogin { get; set; }
        public virtual ICollection<Usuario> Usuarios { get; set; }?????????
    }

Is that what I put, a Collection? And in the user table, only a virtual Idusuario?

  • Why do you need a collection of users? It’s not just a user who does the login? Or I didn’t understand what this table is?

  • @bigown, this, in reality did not know what to put and posted as an example just to know if this is it or not. Lost

2 answers

3


Using your code above, and understanding that you are using Entity Framework Code First. Only fix your code, also understanding that you want to keep a record of all user logins, if it enters the system will generate a record.

public class Usuario
{
    [Key]
    public int Id { get; set; } //Vai ser autoincrement, padrão do entity
    public string NMUsuario { get; set; }
    public string Senha { get; set; }
    public virtual ICollection<Login> Logins { get; set; } //Navigation para ja pegar os registros da tabela login ao consultar os usuarios.
}

public class Login
{
    [Key]
    public int Id { get; set; } //Sequencial somente para efeito de key
    public DateTime DtLogin { get; set; }
    public virtual Usuario Usuario { get; set; } //Aqui se faz a referencia
}

Remembering that you also have to add the two tables in Context.

2

No, you don’t have to wear one Collection. This will make the relationship of Login for Usuarios be 1-N.

The ratio needs to be 1-1, so you only need a property that represents one Usuario within a Login.

[Table("Login")]
public class Login
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int IdLogin { get; set; }
    public DateTime DtLogin { get; set; }
    public virtual Usuario Usuario { get; set; }
}
  • I have seen posts on Soen, with two answers marked. Here I can not do the same? The two answer answers, will depend on how I use and do not want to punish anyone.

  • It doesn’t. Neither here, nor in Soen. And, in fact, the other answer creates a 1-N relationship, which doesn’t seem to be the case with your question.

  • Dude, I’m going hunting and then I show you a two-answer post marked on Soen. But this is irrelevant, I think you’re right, but I haven’t tested everything yet. Anything, the answer that best meet I mark.

Browser other questions tagged

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