删除EF中多对多关系中的选定关系?

时间:2011-03-13 14:08:08

标签: c# asp.net linq entity-framework linq-to-entities

在SQL中,我有:

[乐队] 1 ---- * [BandsGenres] * ---- 1 [类型]

在我的EF图层中,它转换为:

[乐队] * ---- * [类型]

(BandsGenres是BandId和GenreId)

我的问题是,我似乎无法弄清楚如何删除我需要的所选关系。基本上,我正在尝试为乐队更新Genre类型列表。传入Guids列表,我可以使用连接获取。但是,.Remove()方法不会删除旧类型并添加新类型。我尝试使用.Attach(g)和.DeleteObject(g)但是删除了实际的Genre(来自Genres)而不仅仅是关系。

public void UpdateBandGenres(Guid bandId, IEnumerable<Guid> genres)
{
    using (var ctx = new OpenGroovesEntities())
    {
        var newGenres = from g in ctx.Genres
                        join t in genres on g.GenreId equals t
                        select g;

        //var bandGenres = ctx.Genres.Include("Bands").Where(g => g.Bands.Any(b => b.BandId == bandId));
        var bandGenres = ctx.Bands.SingleOrDefault(b => b.BandId == bandId).Genres;

        bandGenres.ToList().ForEach(g =>
        {
            bandGenres.Remove(g);
        });

        newGenres.ToList().ForEach(g => bandGenres.Add(g));

        ctx.SaveChanges();
    }
}

在给定一个类型ID列表的情况下,如何删除/添加或更新乐队的流派关系列表?感谢。

1 个答案:

答案 0 :(得分:3)

如果我理解您的问题,genres集合包含Genre种类列表中因运行Band方法而应该位于UpdateBandGenres种类列表中的所有Guid个对象(而不仅仅是要添加的列表或要删除的列表)。在这种情况下,最简单的方法是从集合中删除所有类型,并使用genres集合中的newGenres添加所有类型。

首先,您不需要加入来抓取var newGenres = ctx.Genres.Where(g => genres.Contains(g.GenreId));

Band

其次,您需要获取Genres对象,因为修改其BandGenres集合将告诉EF修改SQL中的Band band = ctx.Bands.SingleOrDefault(b => b.BandId == bandId); 表:

band.Genres

之后,您可以清除newGenres集合并在顶部添加public void UpdateBandGenres(Guid bandId, IEnumerable<Guid> newGenreIds) { using (var ctx = new OpenGroovesEntities()) { List<Genre> newGenres = ctx. Genres. Where(g => newGenreIds.Contains(g.GenreId)). ToList(); Band band = ctx.Bands.Single(b => b.BandId == bandId); band.Genres.Clear(); newGenres.ForEach(band.Genres.Add); ctx.SaveChanges(); } } 。因此,您的代码将如下所示:

IEnumerable<Guid> genres

顺便说一下,我还建议与命名变量保持一致 - 例如Guid可能有点误导,因为它实际上是Genre的集合,而不是newGenreIds个对象的集合。因此,我将其命名为Guid bandId以与您的{{1}}变量名称保持一致。

相关问题