EF Code First多对多不工作 [英] EF Code First Many-to-Many not working

查看:125
本文介绍了EF Code First多对多不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用实体框架代码第一范式我将以下对象和关系定义为ASP.Net MVC3应用程序的一部分。问题是照片和画廊对象之间的多对多关系无法正常工作。

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.

这是我的代码:

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);
}

通过记录dbCreationScript变量,我可以看到生成的相关sql这些表如下。

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]);

正如你所看到的,没有生成SQL生成查找表,这可能是为什么关系是不工作 - 为什么没有创建一个查找表?

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?

推荐答案

我相信你需要使用这样的东西:

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");
            });
}

这篇关于EF Code First多对多不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆