C# 使用entityframework修改集合

C# 使用entityframework修改集合,c#,entity-framework,C#,Entity Framework,在我的项目中,我有以下实体类 public partial class Movie { public Movie() { this.MovieCategories = new HashSet<MovieCategory>(); } public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } public virtual ICollection

在我的项目中,我有以下实体类

public partial class Movie
{
public Movie()
{
this.MovieCategories = new HashSet<MovieCategory>();
}

public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<MovieCategory> MovieCategories { get; set; }
}

public partial class MovieCategory
{
public int Id { get; set; }
public int CategoryId { get; set; }
public int MovieId { get; set; }
public virtual Category Category { get; set; }
public virtual Movie Movie{ get; set; }
}


public partial class Category
{
public Category()
{
    this.MovieCategories = new HashSet<MovieCategory>();
}
public int Id { get; set; }
public string Category1 { get; set; }
public virtual ICollection<MovieCategory> MovieCategories { get; set; }
这是在更新“Id”、“Name”和“Description”属性,但不更新我的收藏“MovieCategories”


如何更新“MovieCategories”呢?

要修改EF上下文中的关系,上下文应跟踪两个相关对象。这意味着您必须将
Movie
MovieCategory
对象附加到上下文,以修改它们之间的关系

顺便说一句,如果加载实体,它们将在上下文中保持附着。下面是一个简单的示例,您可以使用电影的类别加载电影,清除所有类别并添加新类别:

using (var context = new MovieContext())
{
    var movie = context.Movies.Include(m => m.MovieCategories).First(m => m.Name == "Green Elephant");
    movie.MovieCategories.Clear();
    var newCategory = context.MovieCategories.Create();
    newCategory.Name = "Trash";
    movie.MovieCategories.Add(newCategory);
    context.SaveChanges();
}

例如,您有一个新的电影类别,您想用这个新的电影类别更新一部电影

请注意,我没有测试这个

MovieCategory newCategory = new MovieCategory
{
   "Action Movie"
};

movie.MovieCategories.Add(newCategory);

_movieReviewEntities.Movies.Attach(movie);
_movieReviewEntities.Entry(movie).State = EntityState.Modified;
_movieReviewEntities.SaveChanges();
这还应该添加新的电影类别

如果表中存在您的类别

var existingCategory = _movieReviewEntities.MovieCategories.Where(m => m.Name == "Action Movie")
.Select(m => m).FirstOrDefault();

if(existingCategory != null)
{
   movie.MovieCategories.Add(existingCategory);
}
例如,如果您有一个包含选定电影类别的列表框

//From ListBox
var fromSelection = new List<string>("Comedy", "Drama");

//Fetch from table
var categoryListToAdd = movie.MovieCategories.Where(x => fromSelection.Contains(x.Name)).Select(x => x).ToList();

if(categoryListToAdd.Count > 0)
{
    //Existing categories of the movie.
    var categoryList = movie.MovieCategories.Select(x => x).ToList();


    //Remove the related records.
    if(categoryList.Count > 0)
       categoryList.ForEach(x => _movieReviewEntities.MovieCategories.Remove(x));

    //Then add the selected categories. 
    categoryListToAdd.ForEach(x => _movieReviewEntities.Add(x));
}
//来自列表框
var fromSelection=新列表(“喜剧”、“戏剧”);
//从桌子上取
var categoryListToAdd=movie.MovieCategories.Where(x=>fromSelection.Contains(x.Name)).Select(x=>x.ToList();
如果(categoryListToAdd.Count>0)
{
//电影的现有类别。
var categoryList=movie.MovieCategories.Select(x=>x.ToList();
//删除相关记录。
如果(categoryList.Count>0)
categoryList.ForEach(x=>_movierevieventities.MovieCategories.Remove(x));
//然后添加所选类别。
categoryListToAdd.ForEach(x=>_moviereventities.Add(x));
}

同样,这是没有经过测试的。希望您能理解。

您想从数据库加载
MovieCategories
关系并更新
movie
对象,还是想用本地值更新数据库的值?@astef我想用本地值更新数据库show您创建了“movie”吗对象或如何实例化它?@DeathWish我有编辑视图,可以在其中编辑值并为电影选择多个类别。您的代码不足以理解这个问题。我们可以看电影课。它是数据库中的一个实体还是一个ViewModel?现有的电影类别如何?i、 e.假设我有一个带有电影类别Action的movie对象,现在我想删除moviecategory“Action”并添加moviecategory“喜剧”。这将如何完成?您使用的是什么版本的EF?.RemoveRange和.AddRange将代替迭代。
//From ListBox
var fromSelection = new List<string>("Comedy", "Drama");

//Fetch from table
var categoryListToAdd = movie.MovieCategories.Where(x => fromSelection.Contains(x.Name)).Select(x => x).ToList();

if(categoryListToAdd.Count > 0)
{
    //Existing categories of the movie.
    var categoryList = movie.MovieCategories.Select(x => x).ToList();


    //Remove the related records.
    if(categoryList.Count > 0)
       categoryList.ForEach(x => _movieReviewEntities.MovieCategories.Remove(x));

    //Then add the selected categories. 
    categoryListToAdd.ForEach(x => _movieReviewEntities.Add(x));
}