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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:24:51+00:00 2026-06-01T12:24:51+00:00

I have two tables (Table A, Table B) joined with a join table (TableAB)

  • 0

I have two tables (Table A, Table B) joined with a join table (TableAB) with 3 payload columns. By Payload I mean columns apart from Id, TableAId, and TableBId.

I can insert into all tables successfully, but I need to insert data into one of the payload columns on Insert. I’m using EF 4.3, Fluent API. Can anyone help? Thanks in advance.

    public class Organisation : EntityBase<int>, IAggregateRoot
       {
    public string Name { get; set; }
    public string Url { get; set; }
    public int CountryId { get; set; }
    public int? OwnershipTypeId { get; set; }
    public int OrganisationStatusId { get; set; }

    public virtual ICollection<Feature> Features { get; set; }
    public virtual ICollection<OrganisationType> OrganisationTypes { get; set; }
    public virtual ICollection<PricePlan> PricePlans { get; set; }
    public virtual ICollection<User> Users { get; set; }

}

    public class User: EntityBase<Guid>, IAggregateRoot
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string JobTitle { get; set; }
    public int?  PhoneCallingCodeId       { get; set; }
    public int?  PhoneAreaCode{ get; set; }
    public string PhoneLocal { get; set; }
    public int? MobileCallingCodeId { get; set; }
    public int? MobileAreaCode { get; set; }
    public string MobileLocal { get; set; }      

    public virtual ICollection<Organisation.Organisation> Organisations { get; set; }

}

   public class OrganisationUser : EntityBase<int>, IAggregateRoot
{
    public DateTime StartDate { get; set; }
    public DateTime? EndDate { get; set; }
    public int OrganisationRoleId {get; set;}//Foreign Key - have tried leaving it out, tried it as public virtual Organisation Organisation {get;set;
    public bool IsApproved { get; set; }
}

     public class SDContext : DbContext 
{      

    public ObjectContext Core
    {
        get
        {
            return (this as IObjectContextAdapter).ObjectContext;
        }
    }
  public IDbSet<User> User { get; set; }

  public IDbSet<Organisation> Organisation { get; set; }

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        modelBuilder.Entity<Organisation>().HasMany(u => u.Users).WithMany(o => o.Organisations).Map(m =>
        {
            m.MapLeftKey("OrganisationId");
            m.MapRightKey("UserId");
            m.ToTable("OrganisationUser");
        });

//I have tried specifically defining the foreign key in fluent, but I really need to understand how I can add the payload properties once I access and edit them.

  • 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-01T12:24:53+00:00Added an answer on June 1, 2026 at 12:24 pm

    Your mapping is not correct for your purpose. If you want to treat OrganisationUser as an intermediate entity between Organisation and User you must create relationships between Organisation and OrganisationUser and between User and OrganisationUser, not directly between Organisation and User.

    Because of the intermediate entity which contains its own scalar properties you cannot create a many-to-many mapping. EF does not support many-to-many relationships with “payload”. You need two one-to-many relationships:

    public class Organisation : EntityBase<int>, IAggregateRoot
    {
        // ...
        // this replaces the Users collection
        public virtual ICollection<OrganisationUser> OrganisationUsers { get; set; }
    }
    
    public class User : EntityBase<Guid>, IAggregateRoot
    {
        // ...
        // this replaces the Organisations collection
        public virtual ICollection<OrganisationUser> OrganisationUsers { get; set; }
    }
    
    public class OrganisationUser : EntityBase<int>, IAggregateRoot
    {
        public int OrganisationId { get; set; }
        public Organisation Organisation { get; set; }
    
        public Guid UserId { get; set; }
        public User User { get; set; }
    
        // ... "payload" properties ...
    }
    

    In Fluent API you must replace the many-to-many mapping by the following:

    modelBuilder.Entity<Organisation>()
        .HasMany(o => o.OrganisationUsers)
        .WithRequired(ou => ou.Organisation)
        .HasForeignKey(ou => ou.OrganisationId);
    
    modelBuilder.Entity<User>()
        .HasMany(u => u.OrganisationUsers)
        .WithRequired(ou => ou.User)
        .HasForeignKey(ou => ou.UserId);
    

    Your derived DbContext may also contain a separate set for the OrganisationUser entity:

    public IDbSet<OrganisationUser> OrganisationUsers { get; set; }
    

    It’s obvious now how you write something into the intermediate table:

    var newOrganisationUser = new OrganisastionUser
    {
        OrganisationId = 5,
        UserId = 8,
        SomePayLoadProperty = someValue,
        // ...
    };
    
    context.OrganisastionUsers.Add(newOrganisastionUser);
    context.SaveChanges();
    

    If you want to make sure that each pair of OrganisationId and UserId can only exist once in the link table, it would be better to make a composite primary key of those two columns to ensure uniqueness in the database instead of using a separate Id. In Fluent API it would be:

    modelBuilder.Entity<OrganisationUser>()
        .HasKey(ou => new { ou.OrganisationId, ou.UserId });
    

    More details about such a type of model and how to work with it is here:

    Create code first, many to many, with additional fields in association table

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

Sidebar

Related Questions

I have two tables that are connected via a join table in a many-to-many
I have two tables that get joined regularly. Table One is about 1 Million
I have two tables joined by the foregin key 'ID': table A has ID,MONTH,VOTES
I have two tables that are joined together. A has many B Normally you
I have two tables that should be joined together by a foreign key relationship,
I am building a photo uploading website and I have joined two tables together
I have two tables: Table 1: ID, PersonCode, Name, Table 2: ID, Table1ID, Location,
I have two tables Table public.tags_to_entities Column | Type | Modifiers --------+---------+----------- tid |
I have two tables: Table items: | id | name | description | |
I have two tables: table A id | level_ID | col_m | col_n table

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.