Relationship 1:N with RU

Asked

Viewed 125 times

1

I have a table of units as below:

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; }
}

Each user can have access to 1 or n units. I’ll have a list of the units' records.

var userRight = new List<UsersRight>;
userRight = _DAL.UserRights(user);

var listUser = new List<Unity>; 

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

What is the most efficient way to do this with EF? I am using ASP.NET Identity.

  • From what I understood you would have to create a link table between User and Unit, because a user has access to several Units and a Unit would be connected to several users, correct ?!

  • Your Case seems to me N:N that would have to have a relationship table between them, for example , 2 users have access to the same Unit , but if it is not so the user would have a Icollection of Units, see this link where it shows a relationship 1:N http://www.entityframeworktutorial.net/code-first/configure-one-to-many-relationship-in-code-first.aspx

  • Look at this question as well : http://answall.com/questions/22042/related-n-para-n-e-um-para-n-com-codefirst-data-annotations

  • In fact each user can have 1 or n drives. And each drive can have multiple users

  • So this Relation is N:N, you would need an extra table, making that link, anything I reply explaining how it would be with examples.

  • I also need your drifting class IdentityUser.

  • I will receive the data from the logged in user with the Claims relation.

Show 2 more comments

1 answer

2


In your case with a Unidade may have several Usuarios and a Usuario for having several Unidade, characterizes a relationship N:N, which in the case would have to be done with an extra table making the relationship between the two.

Example :

Unit Table

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

Usuario Table

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

Is and the connection table between the two tables :

    public class UserUnitty
{
     public int UsersRightId {get;set }
     public int UnityId{ get; set; }

     public virtual UsersRight UsersRight{get;set;}
     public virtual Unity Unity{get;set;}

}

Considerations :

  • In the Userunity table, the virtual, the modifier virtual is used by EF to do Lazy Loading, which needs to create proxy instances that will be replaced in these virtual properties. In case when you query these classes will not be filled.
  • I used the UnityId and UserRightId not to use the DataAnnotations, but if you want to keep like the orignal you can just use the DataAnnotations to specify which table belongs to thatForeingKey.
  • Willian, I’m going to have to use Dataannotations, because I’m going to store every unit in the user’s Claims. Then I don’t want to change the structure of the Identity tables.

  • 1

    @b3r3ch1t You have to say that you will use ASP.NET Identity because it changes the entire answer.

  • Ixi, I have no knowledge about Asp.Net Identity to help you, sorry I ended up mistaken myself.

  • 1

    @Williamcézar No, it was no problem in his answer. The author of the question did not specify right to doubt.

  • @Williamscezar sorry if I induced you to a wrong answer. Gypsy has like you send some example that gives me a light?

  • @b3r3ch1t I will give another answer and edit your question.

Show 1 more comment

Browser other questions tagged

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