13 Replies - 1961 Views - Last Post: 08 April 2011 - 12:48 AM Rate Topic: -----

#1 batesy3k  Icon User is offline

  • D.I.C Regular

Reputation: 41
  • View blog
  • Posts: 299
  • Joined: 10-September 09

Entity Model relationship query

Posted 06 April 2011 - 11:12 AM

Hi, I am facing a problem, I have two models (Users and UserContact). The purpose of this is to create a user friend/contact mapping so for example user x is friends with user Y/Z.

Just I am not sure how to create this relationship as the UserID is the "current" user and the UserContactID is the ID for their friend... (which also is the UserID in the Users table)

For example:

UserID | UserContactID
1 2
1 3
1 4
etc...

I have attached a screenshot of my two entity models (these are exactly how the table structure looks as I am using MySQL as the back end)

Any pointers or advice appreciated :)

Attached image(s)

  • Attached Image

This post has been edited by batesy3k: 06 April 2011 - 11:18 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Entity Model relationship query

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5674
  • View blog
  • Posts: 12,189
  • Joined: 02-June 10

Re: Entity Model relationship query

Posted 06 April 2011 - 11:24 AM

So you're saying the UserID changes every time they connect?

You must have some unique identifier for them: You're able to log them in and get their details such as name, address and so on.

I would assume you need to keep a persistent and unique ID for this need. If you don't have a way to consistently identify a user then you can't do what you're trying to do.

Sounds like a lack of planning. Would it be so hard to add a unqueID to each user?
Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4576
  • View blog
  • Posts: 8,018
  • Joined: 08-June 10

Re: Entity Model relationship query

Posted 06 April 2011 - 11:24 AM

Just add the relationships. Both UserId and UserContactId relate directly to UserId, so add that relationship.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5674
  • View blog
  • Posts: 12,189
  • Joined: 02-June 10

Re: Entity Model relationship query

Posted 06 April 2011 - 11:38 AM

View PostCurtis Rutland, on 06 April 2011 - 12:24 PM, said:

Just add the relationships. Both UserId and UserContactId relate directly to UserId, so add that relationship.


I guess you and I are reading this line differently:

Quote

Just I am not sure how to create this relationship as the UserID is the "current" user


Because it was so very obvious to set up the relationship I could not see a reason to post the question unless by "current user id", the OP means this value changes with each log-in... that the value is only good during the current session.

Or the problem could be the OP needs to be able to provide a LIST of friends.

Quote

The purpose of this is to create a user friend/contact mapping so for example user x is friends with user Y/Z.
and a single field of UserContactID doesn't allow for that.

batesy3k: Could you try to give us a better description of what you are doing and the exact nature of the problem?
Was This Post Helpful? 0
  • +
  • -

#5 batesy3k  Icon User is offline

  • D.I.C Regular

Reputation: 41
  • View blog
  • Posts: 299
  • Joined: 10-September 09

Re: Entity Model relationship query

Posted 06 April 2011 - 01:17 PM

Sorry yeah I see your dilemma - should have given a bit more info:

The table "Users" is a table full of people who are registered to the system.

So for example, I would have ID of 1, you would have an ID of 2 which NEVER change as they are auto-increment fields for when they signed up / registerd...

So in that case:

UserID | UserContactID
1 = me | 2 = you
1 = me | 3 = someone else

So if I logged in (UserID of 1) then I would read ContactIDs of 2 and 3 out therefore I would need to pick their values up from the Users table based on the IDs of 2 and 3.

Hope this gives you a bit more insight :)
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5674
  • View blog
  • Posts: 12,189
  • Joined: 02-June 10

Re: Entity Model relationship query

Posted 06 April 2011 - 01:29 PM

View Postbatesy3k, on 06 April 2011 - 02:17 PM, said:

Sorry yeah I see your dilemma - should have given a bit more info:

The table "Users" is a table full of people who are registered to the system.

So for example, I would have ID of 1, you would have an ID of 2 which NEVER change as they are auto-increment fields for when they signed up / registerd...

So in that case:

UserID | UserContactID
1 = me | 2 = you
1 = me | 3 = someone else

So if I logged in (UserID of 1) then I would read ContactIDs of 2 and 3 out therefore I would need to pick their values up from the Users table based on the IDs of 2 and 3.

Hope this gives you a bit more insight :)



That is REALLY straightforward. So what's the problem you're running in to?
Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4576
  • View blog
  • Posts: 8,018
  • Joined: 08-June 10

Re: Entity Model relationship query

Posted 06 April 2011 - 01:49 PM

Again, just add two relationships, both pointing to UserId in the other table.
Was This Post Helpful? 0
  • +
  • -

#8 batesy3k  Icon User is offline

  • D.I.C Regular

Reputation: 41
  • View blog
  • Posts: 299
  • Joined: 10-September 09

Re: Entity Model relationship query

Posted 06 April 2011 - 01:50 PM

Perhaps I'm trying to hard, but struggling to know what relationship types to draw between each entity... Can't seem to get the right ones...

Could you shed some light? :)

Edit: I can get it to go from Users > UserContacts and can retrieve the appropriate UserContactID based on the UserID, just can't figure out how to get the User information (first name etc based on the UserContactID)

This post has been edited by batesy3k: 06 April 2011 - 01:59 PM

Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4576
  • View blog
  • Posts: 8,018
  • Joined: 08-June 10

Re: Entity Model relationship query

Posted 06 April 2011 - 02:03 PM

Well, my first question would be, are you generating this model from the database? If so, add the relationship there, and let it be reflected by your entity model.

Also, UserContact needs a primary key. I'd say just call it Id.

Anyway, it would be a many to many relationship from UserContact.UserId to User.UserId, and a many to many from UserContact.UserContactId to User.UserId.

This post has been edited by Curtis Rutland: 06 April 2011 - 02:04 PM

Was This Post Helpful? 0
  • +
  • -

#10 batesy3k  Icon User is offline

  • D.I.C Regular

Reputation: 41
  • View blog
  • Posts: 299
  • Joined: 10-September 09

Re: Entity Model relationship query

Posted 06 April 2011 - 02:17 PM

I am generating the model from the database which is MySQL so I not sure if you can add relationships via phpmyadmin?

I have tried that (see screenshot):

but I get this error when running:

Quote

Error 1 Error 3027: No mapping specified for the following EntitySet/AssociationSet - UserUserContact, UserContactUser. C:\Development\programming\Visual Studio 2010\Projects\_wrs\ProjectCMS\ProjectCMS.Data\Repository\CmsModel.edmx 363 9 ProjectCMS.Data


Do I need to setup somewhere what maps to what?

Sorry I am new to this EF modelling, but it's something I want to crack :)

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#11 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4576
  • View blog
  • Posts: 8,018
  • Joined: 08-June 10

Re: Entity Model relationship query

Posted 06 April 2011 - 02:30 PM

The second response in this SO thread:

http://stackoverflow...nships-in-mysql

Shows how to alter a table to include a foreign key. So just add a key for each relationship, then regenerate the Entity Model.

You'll just have to give the keys unique names.
Was This Post Helpful? 1
  • +
  • -

#12 batesy3k  Icon User is offline

  • D.I.C Regular

Reputation: 41
  • View blog
  • Posts: 299
  • Joined: 10-September 09

Re: Entity Model relationship query

Posted 06 April 2011 - 03:05 PM

Yeah putting those relationships in via MySQL workbench did a job :)

Did you say they need to be many to many? because it has them as one-to-many (one being the Users entity end)
Was This Post Helpful? 0
  • +
  • -

#13 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4576
  • View blog
  • Posts: 8,018
  • Joined: 08-June 10

Re: Entity Model relationship query

Posted 06 April 2011 - 03:33 PM

I'm not actually sure which relationship it is. One-to-many sounds more correct.
Was This Post Helpful? 0
  • +
  • -

#14 batesy3k  Icon User is offline

  • D.I.C Regular

Reputation: 41
  • View blog
  • Posts: 299
  • Joined: 10-September 09

Re: Entity Model relationship query

Posted 08 April 2011 - 12:48 AM

Thank you for your time and tlhIn`toq your time too :)

It's nice to be able to come to a professional forum where people want to help and encourage :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1