Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6955649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:45:53+00:00 2026-05-27T14:45:53+00:00

I am working on a project with the following conditions: Visual Studio 2010 ASP.NET

  • 0

I am working on a project with the following conditions:

  • Visual Studio 2010
  • ASP.NET MVC 3
  • EF 4.1 (can use something else if recommended)
  • Code first

I am trying to model the following, but I stuck in my thinking on how to do it best.

I have these objects.

public class Facility
{
    public virtual int FacilityId;
    public virtual string Name;
    public virtual List<TaskCategory> TaskCategories;
}

public class TaskCategory
{
    public virtual int TaskCategoryId;
    public virtual string Name;
}

public class User
{
    public virtual int UserId;
    public virtual string Username;
}

Facility and TaskCategory is a many-to-many relationship
Facility and User is a one-to-many relationship (one facility can have many users, one user can belong only to one facility)

Now I need some way to connect these three objects so that the following conditions are met:
– In the system, one should be able to connect a user to a certain facility AND certain TaskCategory

In a traditional database I would model it like this:

User_id, Facility_id, TaskCategory_id
1,       1,           1
1,       1,           2
2,       1,           1
1,       2,           1

Which means that user 1 will have access to TaskCategories 1 and 2 in Facility 1 and TaskCateogry 1 in Facility 2.
User 2 will have access to TaskCategory 1 in Facility 1.

Does this make sense, and how would I do this in an object-oriented environment that works with EF 4.1 (or other ORM).

UPDATE:
The following code is what I ended up using (some irrelevant pieces not included here):

public class Facility
{
    public int Id { get; set; }
    public string Name { get; set; }

    private ICollection<FacilityMembership> _facilityMembership;
    public virtual ICollection<FacilityMembership> FacilityMembership
    {
        get { return_facilityManager ?? (_facilityManager = new HashSet<FacilityMembership>(); }
        set { _facilityManager = value; }
    }
}

}

public class TaskCategory
{
    public int Id { get; set; }
    public string Name { get; set; }

    private ICollection<FacilityMembership> _taskMemberships;
    public virtual ICollection<FacilityMembership> TaskMemberships
    {
        get { return _taskMemberships?? (_taskMemberships= new HashSet<FacilityMembership>()); }
        set { _taskMemberships = value; }
    }
}

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

    private ICollection<FacilityMembership> _facilityMembership;
    public virtual ICollection<FacilityMembership> FacilityMembership
    {
        get { return_facilityManager ?? (_facilityManager = new HashSet<FacilityMembership>(); }
        set { _facilityManager = value; }
    }
}

public class FacilityMembership
{
    public int Id { get; set; }
    public int FacilityId { get; set; }
    public int UserId { get; set; }
    private ICollection<TaskCategory> _taskCategories;
    public virtual ICollection<TaskCategory> TaskCategories
    {
        get { return _taskCategories ?? (_taskCategories = new HashSet<TaskCategories>()); }
        set { _taskCategories = value; }
    }
}

And then mapping via fluent api:

        modelBuilder.Entity<FacilityMembership>().HasKey(fm => fm.Id);
        modelBuilder.Entity<FacilityMembership>()
                    .HasMany(fm => fm.TaskCategories)
                    .WithMany(tc => tc.FacilityMemberships)
                    .Map(m =>
                             {
                                 m.MapLeftKey("FacilityMembershipId");
                                 m.MapRightKey("TaskCategoryId");
                             });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:45:54+00:00Added an answer on May 27, 2026 at 2:45 pm

    Your traditional database model suggests it is a many-to-many relationship (user to facility) with additional properties (tasks). There is no magic way to do this in EF, it’s simply the same as you would in a database, with an extra table/entity.

    public class User {
      ICollection<FacilityTask> FacilityTask {get; set;}
    }
    
    public class FacilityTask {
      public Facility Facility {get; set;}
      public Task Task {get; set;}
    }
    
    or 
    
    public class FacilityTasks {
      public Facility Facility {get; set;}
      public ICollection<Task> Task {get; set;}
    }
    

    There may be a better naming scheme than FacilityTasks, perhaps a FacilityMembership? A user belongs to a Facility and there are tasks associated to that membership.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a PHP project. There, I often use following syntax to
I'm working on the following project: http://lpmj.net/20.php I've made several entries into phpMyadmin and
I'm working on getting my ASP.Net project working on Linux. I've been testing my
I am working on this mvc project following Rob Connery's storefront video series and
I'm working on a project which uses the following technologies: C# (.NET 4.0) WCF
I have this project I'm working on. The following conditions apply In this project
In Visual Studio 2008 I just added the following lines to an existing (working)
I am working on a project and have the following psuedocode: // create a
In a project that I am working on, I have the following entities: Analyst
I'm working for a university project, and I have the following question: I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.