Note that there are some explanatory texts on larger screens.

plurals
  1. POEF Code First Many-to-Many not working
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>I would very much appreciate your solutions on how to make this many-to-many relationship work.</p> <p>Here's my code:</p> <pre><code>public class Photo { public int ID { get; set; } public string Title { get; set; } public virtual ICollection&lt;Gallery&gt; Galleries { get; set; } } public class Gallery { public int ID { get; set; } public string Title { get; set; } public virtual ICollection&lt;Photo&gt; 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&lt;Photo&gt;().HasMany&lt;Gallery&gt;(p =&gt; p.Galleries); modelBuilder.Entity&lt;Gallery&gt;().HasMany&lt;Photo&gt;(g =&gt; g.Photos); } </code></pre> <p>I am using the following code within my own custom database initializer.</p> <pre><code>public void InitializeDatabase(PhotoDBContext context) { var dbCreationScript = ((IObjectContextAdapter)context).ObjectContext.CreateDatabaseScript(); Log(dbCreationScript); context.Database.ExecuteSqlCommand(dbCreationScript); } </code></pre> <p>By logging the dbCreationScript variable I can see that the relevant sql that is generated for these tables is as follows.</p> <pre><code>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]); </code></pre> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload