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 7879373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:53:08+00:00 2026-06-03T03:53:08+00:00

I am working on a project with ASP.Net MVC3 EF4.1 and relationships between tables

  • 0

I am working on a project with ASP.Net MVC3 EF4.1 and relationships between tables through foreign keys. I am using the database first approach and have three tables: Calendar, Calendar Users and Users. The relationship is that a Calendar can have many users and users can have many calendars.

When someone is creating the calendar he/she is also supposed to select the number of users that will have access to the calendar. But now when I am about to save the changes to the database in the controller thats when I get confused. In the generated classes there are also virtual ICollections that I suppose represent the foreign keys somehow. But I can’t figure out how I am supposed to handle them? So how is it supposed to work? Should I be able to add the changes to the virtual ICollections and then just do db.SaveChanges() and it will work by itself or am I supposed too handle that manually?

If I am supposed to handle it manually should I then add the users, add the calendar and then add the keys in the CalendarUsers table to bind them together? I’ve seen some examples from code first where they have clarified the relationship by entering code in the OnModelCreating method but when using Database first it just contains: throw new UnintentionalCodeFirstException();? Hoping you perhaps can clarify it for me a bit.

Added the classes generated by the DBcontext Generator below:

public partial class Calendar
{
    public Calendar()
    {
        this.CalendarUsers = new HashSet<CalendarUser>();
    }

    public int CalendarId { get; set; }
    public string CalendarTitle { get; set; }
    public string CalendarDescription { get; set; }
    public long UserId { get; set; }

    public virtual User User { get; set; }
    public virtual ICollection<CalendarUser> CalendarUsers { get; set; }
}

public partial class CalendarUser
{
    public int CalendarUserId { get; set; }
    public int CalendarId { get; set; }
    public long UserId { get; set; }
    public Nullable<bool> IsAdmin { get; set; }

    public virtual Calendar Calendar { get; set; }
    public virtual User User { get; set; }
}

public partial class User
{
    public User()
    {
        this.Calendars = new HashSet<Calendar>();
        this.CalendarUsers = new HashSet<CalendarUser>();
    }

    public long UserId { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Calendar> Calendars { get; set; }
    public virtual ICollection<CalendarUser> CalendarUsers { get; set; }
}
  • 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-06-03T03:53:09+00:00Added an answer on June 3, 2026 at 3:53 am

    You can just create a new relationship between existing Calendar and User by setting the foreign key properties in a new instance of the CalendarUser:

    var newCalendarUser = new CalendarUser
    {
        CalendarId = calendarId,
        UserId = userId,
        IsAdmin = true // or false
    };
    dbContext.CalendarUsers.Add(newCalendarUser);
    dbContext.SaveChanges();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a web application project using ASP.NET MVC3 and database in SQL
My team working in a project using asp.net mvc3(c#). Based on the project requirement,we
I working on asp.net mvc3 project. I am using autofac for DI. I have
I am currently working on a C# project using ASP.NET. And I'd like to
We are working on a ASP.NET project and we are using the resx files
I am working on a CMS project using ASP.Net 3.5/Visual studio 2008.This is the
I am working on asp.net MVC 3 project. I am using EF 4.1 code
I am working on a project in ASP.Net, and I am using SQL Server
I am working on a c# project using ASP .net. I have a list
Currently I'm working on Web project that is built using ASP.NET Web Forms. We

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.