Question

I am using petapoco relationExtensions with the following :

Connection.db.FetchOneToMany<Project, UserProject>(x=>x.ID, string.Format(@"
            SELECT * 
            FROM Project                         
            WHERE project.id = userProject.ProjectID
                AND userProject.UserID =" + userID + 
                 " ORDER BY project.Name" )); 

but having the following error.

 > The multi-part identifier "userProject.ProjectID" could not be bound. The multi-part identifier "userProject.UserID" could not be bound.

What is wrong? Am I missing anything?

Project Class definition

    private int id;
        public  int ID
        {
            get { return id; }
            set { id = value; }
        }
        private string name;
 public string Name
        {
            get { return name; }
            set { name = value; }
        }

UserProject Class definition

int id;
    int userId;
           int projectId;
 public int ID
    {
        get { return id; }
        set { id = value; }
    }
 public int UserID
    {
        get { return userId; }
        set { userId = value; }
    }
 public int ProjectID
    {
        get { return projectId; }
        set { projectId = value; }
    }
Was it helpful?

Solution

Assuming that there is a UserProject table; the error is a SQL error because you're not joining the UserProject table and referring to it in the sql. Try this :

Connection.db.FetchOneToMany<Project, UserProject>(x=>x.ID, string.Format(@"
            SELECT * 
            FROM Project                   
            left join userproject on userProject.ProjectID = project.id
                where userProject.UserID =" + userID + 
                 " ORDER BY project.Name" )); 

The relation extensions don't change your sql

OTHER TIPS

Try this. It specifies the second object's key as well as the first.

Notice also the usage of parameters rather than concatenating it into your sql string. That wouldn't give you an error, but it's better to do it this way for a variety of reasons.

Edit

I just noticed that you are not actually joining in the UserProject table in your sql. That would be why you got the first error above. Did you really mean something like below?

var projects = Connection.db.FetchOneToMany<Project, UserProject>(
               x => x.ID, 
               "SELECT * 
               FROM Project
               LEFT JOIN UserProject ON project.id = userProject.ProjectID                        
               WHERE userProject.UserID = @0 
               ORDER BY project.Name", userID);

Update

Based on your classes above, you are missing something. I am assuming that you have a one-to-many relationship between Projects and UserProjects (seems like UserProject might be a bridge table in a many-to-many relationship between Projects and Users). The purpose of the FetchOneToMany method is to take the 'many' side of the relationship and put all those objects into a list property on the 'one' side of the relationship.

So, you should have a property on your Project class that is List<UserProject> type in order for this to work.

public class Project
{
    public int ID { get; set; }
    public string Name { get; set; }
    public List<UserProject> UserProjects { get; set; }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top