13

I have the following model

public class PageConfig : Base
{
    // Properties Etc..

    public ICollection<Image> ScrollerImages { get; set; }
}

My approach is to bind using a junction table { PageConfigID, ImageID }.

In my model binder i tried the following..

modelBuilder.Entity<PageConfig>()
    .HasMany(x => x.ScrollerImages)
    .WithMany()
    .Map(x =>
    {
        x.ToTable("junc_PageConfigScrollerImages");
        x.MapLeftKey("PageConfigID");
        x.MapRightKey("ImageID");
    });

Which results in a null collection of images.

How can i bind these Images to the PageConfig model?

EDIT

Most of the problem was due to user error. jic this happens to you..

Check that the key constraints in the database are correctly set.
The ICollection on the model NEEDS to be virtual.

opdb
  • 221
  • 1
  • 3
  • 12

2 Answers2

9

If you want to create an one-to-many relationship between those two entities your model would be like this:

public class PageConfig
{
    public int Id {get;set;}

    //navigation property
    public ICollection<Image> ScrollerImages {get;set;}
}

public class Image 
{
    public int Id {get;set;}

    //FK
    public int? PageConfigId {get;set;}

    //navigation property
    public PageConfig PageConfig {get;set;}
}

And the Fluent Api configuration would be:

modelBuilder.Entity<Image>()
            .HasOptional(i=>i.PageConfig)
            .WithMany(pc=>pc.ScrollerImages)
            .HasForeignKey(i=> i.PageConfigId);

If you idea is create an unidirectional one-to-many relationship then delete the FK and the navigation property on Image entity and configure the relationship this way:

modelBuilder.Entity<PageConfig>()
            .HasMany(pc => pc.ScrollerImages)
            .WithOptional();

Check this link for more info about this kind of relationship

ocuenca
  • 38,548
  • 11
  • 89
  • 102
  • unidirectional one-to-many relationship is what i am after. HasOptional doesnt appear to have a no argument constructor, im not sure what to do there. – opdb May 12 '15 at 22:26
  • I thought It could be in any order, I have changed that piece of code, check now – ocuenca May 12 '15 at 23:01
  • Turns out there were more problems under the surface, but everything is running smooth now. Since your answer covers the common scenarios it is correct. Thanks. – opdb May 12 '15 at 23:23
1

Per http://www.entityframeworktutorial.net/code-first/configure-one-to-many-relationship-in-code-first.aspx:

"...you can use Fluent API to configure a One-to-Many relationship using Student entity classes as shown below."

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
            //one-to-many 
            modelBuilder.Entity<Student>()
                        .HasRequired<Standard>(s => s.Standard)
                        .WithMany(s => s.Students)
                        .HasForeignKey(s => s.StdId);

    }

"...Use HasOptional method instead of HasRequired method to make foreign key column nullable."

So you'd be looking for something like this:

modelBuilder.Entity<Image>()
            .HasOptional<PageConfig>(x => x.PageConfig)
            .WithMany(x => x.ScrollerImages)
            .HasForeignKey(x => x.PageConfigId)
colinsmith1
  • 145
  • 7
  • Image does not know anything about PageConfig, so i am not sure how to use your example. A single image can be used by many PageConfig, and an Image does not need to know about PageConfig in any way. – opdb May 12 '15 at 21:59