如何在EF中配置一对多关系

时间:2021-12-18 06:46:43

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 }.

我的方法是使用联结表{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?

如何将这些图像绑定到PageConfig模型?

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.

检查数据库中的键约束是否已正确设置。模型上的ICollection NEEDS是虚拟的。

2 个解决方案

#1


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:

而Fluent Api配置将是:

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:

如果您的想法是创建单向一对多关系,则删除Image实体上的FK和导航属性,并以这种方式配置关系:

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

Check this link for more info about this kind of relationship

有关此类关系的详细信息,请查看此链接

#2


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."

“...您可以使用Fluent API使用Student实体类配置一对多关系,如下所示。”

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."

“...使用HasOptional方法而不是HasRequired方法使外键列可以为空。”

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)

#1


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:

而Fluent Api配置将是:

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:

如果您的想法是创建单向一对多关系,则删除Image实体上的FK和导航属性,并以这种方式配置关系:

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

Check this link for more info about this kind of relationship

有关此类关系的详细信息,请查看此链接

#2


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."

“...您可以使用Fluent API使用Student实体类配置一对多关系,如下所示。”

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."

“...使用HasOptional方法而不是HasRequired方法使外键列可以为空。”

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)