b3r3ch1t
b3r3ch1t

Reputation: 165

Relationship 1:N With EF

I have this table

public class Unity
{
    public int Id {get;set }
    public string Name{ get; set; }
}

public class UsersRight
{
    public int Id {get;set }        
    public string Name{ get; set; }
    public int Value{ get; set; }
}

I need a list of all unities that user have access.

I know to do this way:

var userRight = _DAL.UserRights(user).ToList();

var listUser = new List<Unity>; 

foreach (var item in userRight)
{ 
  listUser.add( new Unity(Name = item.Name, Id = item.Value));
}

How can I do this in a more efficient way?

Upvotes: 0

Views: 119

Answers (1)

Mihaela Diaconu
Mihaela Diaconu

Reputation: 116

In your scenario, the User entity should have a list of Unities:

public virtual ICollection<Unity> Unities { get; set; }

and the Unity entity should have a User:

public virtual User User { get; set; }

You can check this entity framework tutorial on how to configure one-to-many relationship.

Upvotes: 3

Related Questions