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

  • Home
  • SEARCH
  • 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 6044793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:03:07+00:00 2026-05-23T07:03:07+00:00

Using the Entity Framework Code First paradigm I have defined the following objects and

  • 0

Using the Entity Framework Code First paradigm I have defined the following objects and relationships as part of an ASP.Net MVC3 application. The problem is the many-to-many relationship between the Photo and Gallery objects is not working properly.

Currently a gallery can contain many photos – this is correct. But a photo can only be associated with one Gallery – this is incorrect. I want a photo to be able to be in many galleries. If I add a photo to a gallery when it is already associated with another gallery, it is removed from the other gallery.

I would very much appreciate your solutions on how to make this many-to-many relationship work.

Here’s my code:

public class Photo
{
    public int ID { get; set; }
    public string Title { get; set; }
    public virtual ICollection<Gallery> Galleries { get; set; }
}

public class Gallery
{
    public int ID { get; set; }
    public string Title { get; set; }
    public virtual ICollection<Photo> Photos { get; set; }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    /* I would expect the following to cause a lookup table to 
     *  be created but it isnt */
    modelBuilder.Entity<Photo>().HasMany<Gallery>(p => p.Galleries);
    modelBuilder.Entity<Gallery>().HasMany<Photo>(g => g.Photos);
}

I am using the following code within my own custom database initializer.

public void InitializeDatabase(PhotoDBContext context)
{
    var dbCreationScript = ((IObjectContextAdapter)context).ObjectContext.CreateDatabaseScript();
    Log(dbCreationScript);
    context.Database.ExecuteSqlCommand(dbCreationScript);
}

By logging the dbCreationScript variable I can see that the relevant sql that is generated for these tables is as follows.

create table [dbo].[Galleries] (
    [ID] [int] not null identity,
    [Title] [nvarchar](max) null,
    [Photo_ID] [int] null,
    primary key ([ID])
);

create table [dbo].[Photos] (
    [ID] [int] not null identity,
    [Title] [nvarchar](max) null,
    [Gallery_ID] [int] null,
    primary key ([ID])
);

alter table [dbo].[Photos] add constraint [Gallery_Photos] foreign key ([Gallery_ID]) references [dbo].[Galleries]([ID]);
alter table [dbo].[Galleries] add constraint [Photo_Galleries] foreign key ([Photo_ID]) references [dbo].[Photos]([ID]);

As you can see there is no SQL generated to create a lookup table which is probably why the relationship is not working – why isnt a lookup table being created?

  • 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-23T07:03:08+00:00Added an answer on May 23, 2026 at 7:03 am

    I believe you need to use something like this:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
            modelBuilder.Entity<Photo>()
                .HasMany<Gallery>(x => x.Galleries)
                .WithMany(x => x.Photos)
                .Map(x =>
                {
                    x.MapLeftKey("ID");
                    x.MapRightKey("ID");
                    x.ToTable("GalleryPhotos");
                });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using MVC3 with Entity Framework Code First. I have the following model.
I have been using Entity Framework CTP with Code-First as in this tutorial by
First some brief background: I have an existing ASP.NET MVC 1 application using Entity
I am learning ASP.NET MVC and Entity Framework Code First, LINQ by creating a
I'm using Entity Framework 4 Code First in my .net MVC 2.0 project and
Using the Entity Framework 4. I have created a Code First database in the
I'm using Entity Framework 4.1 Code First. In my entity, I have three date/time
I have a problem with the following Linq query using Entity Framework: from o
Okay, so I'm doing my first foray into using the ADO.NET Entity Framework. My
I'm using code-first in entity-framework 4, Entity Framework Feature CTP 3 Is it possible

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.