Hey all. I have three tables in my EF Model; User, Activity and UserActivity
The form in my system will allow a user to associate their account with an activity that exists in the system by selecting one from a DropDownList. If the user selects the final value in the DropDownList, "Add New...", then another form appears for them
to add a new Activity.
I know how to add Activities to the database, but I'm not too sure about creating the link between the two tables. The third field, MonthsInActivity, kinda throws me off. I thought there was a clever way to automatically associate an Activity to a User...
something like accessing the Activities Entity from the User Entity... but at best, I can only get to UserActivity from both Entities.
Can someone clarify this or at least say what type of pattern/practice I'm trying to achieve so I know what to specifically search for? Thanks in advance.
know how to add Activities to the database, but I'm not too sure about creating the link between the two tables. The third field, MonthsInActivity, kinda throws me off.
To this problem, I think when you create a WinForm or a WebForm that has two kinds of Dropdownlist, one is bound to all activities, while the other is bound with Users. And you should put a TextBox for "MonthsInActivity". Thus you can choose one for Activity
instance, while the another for user instance. And fill into the Textbox with value and add UserActivity.
Thanks Decker for the reply. I'm more confused with the EF.
In one instance I need to associate an Activity to a User, along with the MonthsInActivity. Looking at the EF there is a link between Activity and User in the UserActivity table. If the Activity and User both exist already, do I just do this:
public void AssociateActivityToUser(Activity Activity, User User, int MonthsInActivity)
{
using (Entities context = new Entities())
{
UserActivity ua = new UserActivity();
ua.ActivityReference.Attach(Activity);
ua.UserReference.Attach(User);
ua.MonthsInActivity = MonthsInActivity;
context.UserActivities.AddObject(ua);
context.SaveChanges();
}
}
...or do I do something more like this:
public void AssociateActivityToUser(int ActivityId, Guid UserId, int MonthsInActivity)
{
using (Entities context = new Entities())
{
UserActivity ua = new UserActivity();
ua.ActivityIdFk = ActivityId;
ua.UserIdFk = UserId;
ua.MonthsInActivity = MonthsInActivity;
context.UserActivities.AddObject(ua);
context.SaveChanges();
}
}
There is also the possibility that the Activity won't exist until the user enters it. I figure there are two options... The first is where I add the activity to the database first, then associate it to a user using one of the above (or something like them
if I'm doing it wrong) method. The other option is to, in the same Context (if its possible), to add the Activity and automatically associate it to a user with the MonthsInActivity value. I know when doing a simple Many-to-Many, you can do something like
this: User.Activity.Add(), and that'll automatically create the association... but, like I mentioned before, that extra field in the middle table is really screwing me up.
Rockstarter
Member
31 Points
52 Posts
How do you handle manipulation with this schema setup?
Dec 15, 2012 04:05 PM|LINK
Hey all. I have three tables in my EF Model; User, Activity and UserActivity
The form in my system will allow a user to associate their account with an activity that exists in the system by selecting one from a DropDownList. If the user selects the final value in the DropDownList, "Add New...", then another form appears for them to add a new Activity.
I know how to add Activities to the database, but I'm not too sure about creating the link between the two tables. The third field, MonthsInActivity, kinda throws me off. I thought there was a clever way to automatically associate an Activity to a User... something like accessing the Activities Entity from the User Entity... but at best, I can only get to UserActivity from both Entities.
Can someone clarify this or at least say what type of pattern/practice I'm trying to achieve so I know what to specifically search for? Thanks in advance.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: How do you handle manipulation with this schema setup?
Dec 16, 2012 03:38 AM|LINK
Hi,
To this problem, I think when you create a WinForm or a WebForm that has two kinds of Dropdownlist, one is bound to all activities, while the other is bound with Users. And you should put a TextBox for "MonthsInActivity". Thus you can choose one for Activity instance, while the another for user instance. And fill into the Textbox with value and add UserActivity.
Rockstarter
Member
31 Points
52 Posts
Re: How do you handle manipulation with this schema setup?
Dec 16, 2012 01:08 PM|LINK
Thanks Decker for the reply. I'm more confused with the EF.
In one instance I need to associate an Activity to a User, along with the MonthsInActivity. Looking at the EF there is a link between Activity and User in the UserActivity table. If the Activity and User both exist already, do I just do this:
public void AssociateActivityToUser(Activity Activity, User User, int MonthsInActivity) { using (Entities context = new Entities()) { UserActivity ua = new UserActivity(); ua.ActivityReference.Attach(Activity); ua.UserReference.Attach(User); ua.MonthsInActivity = MonthsInActivity; context.UserActivities.AddObject(ua); context.SaveChanges(); } }...or do I do something more like this:
public void AssociateActivityToUser(int ActivityId, Guid UserId, int MonthsInActivity) { using (Entities context = new Entities()) { UserActivity ua = new UserActivity(); ua.ActivityIdFk = ActivityId; ua.UserIdFk = UserId; ua.MonthsInActivity = MonthsInActivity; context.UserActivities.AddObject(ua); context.SaveChanges(); } }There is also the possibility that the Activity won't exist until the user enters it. I figure there are two options... The first is where I add the activity to the database first, then associate it to a user using one of the above (or something like them if I'm doing it wrong) method. The other option is to, in the same Context (if its possible), to add the Activity and automatically associate it to a user with the MonthsInActivity value. I know when doing a simple Many-to-Many, you can do something like this: User.Activity.Add(), and that'll automatically create the association... but, like I mentioned before, that extra field in the middle table is really screwing me up.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: How do you handle manipulation with this schema setup?
Dec 17, 2012 01:02 AM|LINK
Hi again,
Just take a look at your image, I think you can either:
1) Find an existing instance of Activity first.
2) Use its NavigateProperty of "UserActivities".
3) Create another instance of "UserActivities", with another instance of "User" assigned to the navigator of itself.