2

I'm new to MVC, and this confusing me for a long time but seems few questions discuss about this.

I'm using Model first creating Database in ASP.NET MVC.
If I want a Member relationship between User and Project ,
means which user has joined to which project.
And the member relationship should have additional attributes contribution and joinDate.

I created a model like this. (Sorry for chinese)

enter image description here

Both User and Project have a navigation attribute Member.

But the main problem is, I don't know how to use it.

If I don't need the Contribution and JoinDate attributes, I could easily just map the User and Project relationship like this (again, sorry):

enter image description here

And this could work very intuitively by maybe

user.projects.Add(project);

Now I have the additional attributes in the relationship, I don't know how to work in this way. It's very not intuitive.

// The user join to the project.
Member member = new Member(user,project);  // create a relationship manually
member.contribution = 500;
user.members.Add(member);

As well as to return the user list and projects each user owns.

var userSet = from user in db.UserSet
              select new
              {
                  id = user.Id,
                  name = user.Name,
                  projectList = user.Member.Select(m => new
                  {
                      id = m.Project.Id,
                      name = m.Project.Name
                  })
              };

return Json(userSet, JsonRequestBehavior.AllowGet);

Is there any way to improve this? Please share your idea. (Thanks for patience.)

OOD Waterball
  • 707
  • 1
  • 11
  • 31

1 Answers1

1

We faced the same problem as we wanted to save the signature of every person who participates a test. We did this little workaround (Sorry for the bad design I created this in paint to match your needs):

ER-Diagram

Even there are Many-To-Many connections every "Member" only have 1 User and 1 Project per entry. We access them with Member.Users.First().DoSomething();. I know this is not the cleanest way and any database architect wants to kill me if he sees this but it works and you don't have to create your entries manually.

T. Jung
  • 3,327
  • 3
  • 11
  • 19