3

Models:

 public class User
 {
     [Key]
     public int UserId { get; set; }
     public string UserName { get; set; }
 }

public class Resource
{
    [Key]
    public int ResourceId { get; set; }
    public string ResourceName { get; set; }
    public string  ResourceDescription { get; set; }
}

public class UserResource
{
    [Key, Column(Order=0)]
    public int UserId { get; set; }
    [Key, Column(Order=1)]
    public int ResourceId { get; set; }
    public int ResourceQuantity { get; set; }
}

I want to select "ResourceName" from Resource model and "ResourceQuantity" from UserResource model for a given "UserId". Also, once selected, do I need a brand new model to carry only those two specified columns?

Also note that UserResource model has a composite key so I am confused as to how to make the join... Is this right?

 var userResources =
          from r in imDB.Resources
          join ur in imDB.UserResources
          on r.ResourceId equals ur.ResourceId
          select new { r.ResourceName, ur.ResourceQuantity };
4
  • can you explain your business scenario bit more ? If you need to use composite key (as a junction model) when you have to have M:M relationship.What is your other model which having many relationship ? Commented Feb 9, 2013 at 14:12
  • Hi, my other model is a very simple one called User. The UserResource model joins the User and Resource models. I updated the models. Commented Feb 9, 2013 at 14:22
  • do you having code first or what ? Which EF version you're using ? Commented Feb 9, 2013 at 14:31
  • yes Code First, and latest EF (5) Commented Feb 9, 2013 at 14:35

1 Answer 1

3

Hence you're using Code first you can create your models are as below by using EF conventions.

public class User {
    public int Id { get; set; }
    public string UserName { get; set; }

    public virtual ICollection<Resource> Resources { get; set; }
   }

public class Resource {
    public int Id { get; set; }
    public string ResourceName { get; set; }
    public int ResourceQuantity { get; set; }

    public virtual ICollection<User> Users {get;set;}
}

Then EF will generate your junction table is as UsersResources.You don't need to create additional model as you did.EF will look after that.

When using POCOs with EF, if you mark your navigation properties as virtual you can use additional EF supports like Lazy Loading. So in general use a virtual keyword in navigation properties considered to be a good practice.

UPDATE

You may try something like below:

Method 1 : Method based syntax

imDB.Resources.Where(r => r.Users.Any(u => u.UserId == userId))

Method 2 : Query based syntax

from r in imDB.Resources
from u in r.Users
where u.UserId == userId
select r;

I hope this will help to you.

Sign up to request clarification or add additional context in comments.

6 Comments

Wow thank you, does this mean I can get rid of the UserResources model? Although one thing I noticed is that you put "ResourceQuantity" in the Resource model, each user should have their own copy of the quantity of any resource.
@Deniz could you explain the relationship between "user" and "ResourceQuantity" ? is that 1:M or M:M ?
A user can have many different resources (by differing resourceId). A user can have a varying quantity of each different kind of resource (Indicated by the ResourceQuantity column).
@Deniz yes.I feel I am right.So You can get rid of "UserResource" model.Try this and let me know if you're having any issues ?
Thanks so much for your help so far Sampath, I really appreciate it that people are so helpful on this site. I am trying to write the code to return the Resource objects for a given user but I am getting squiggly error for the UserId bit. return imDB.Resources.Where(r => r.Users.UserId == userId);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.