0

这是我的班级编队:

namespace CVtheque.Entities
{
    public class Formation
    {

        public Formation()
        {
            this.CVs = new HashSet<CV>();
        }

        public int Id { get; set; }
        public string name { get; set; }

        public virtual ICollection<CV> CVs { get; set; }

    }
}

这是我的班级简历

namespace CVtheque.Entities
{
    public class CV
    {
        public CV()
        {
            this.Formations = new HashSet<Formation>();
        }

        public int Id { get; set; }
        public string Name { get; set; }

        public virtual ICollection<Formation> Formations { get; set; }

        [InverseProperty("CVs")]
        public Personne Personne { get; set; }

    }
}

我从这两个类创建了一个 DBContext,并自动创建了“join”表 FormationsCVs。

在网页上,用户可以启用或禁用每个 CV 的某些 Formations。请问如何在控制器中使用 linq 语法清除表格 FormationCVs ?以及如何插入新人?(CV1Id, Formation1Id), (CV1Id, Formation2Id) 等等..

4

3 回答 3

0

如果要删除:

            db.FormationCVs.Attach(obj); 
            db.FormationCVs.DeleteObject(obj);
            db.SaveChanges();

如果要添加:

            db.FormationCVs.Add(obj)
            db.SaveChanges();
于 2019-03-18T13:52:36.117 回答
0

感谢您的回复。我会试一试,但在此之前我非常接近使用此代码的解决方案:

// many to many add or delete


using (Context context = new Context())
                {

                    var existingCV = (from c in context.CVs
                                      where c.Id == cv.Id select c).FirstOrDefault();

                    if(formationsIds != null)
                    {
                         var clientSideFormations = context.Formations
                        .Where(t => formationsIds.Contains(t.Id));

                        var deletedFormations = existingCV.Formations.Except(clientSideFormations).ToList<Formation>();

                        var addedFormations = clientSideFormations.Except(existingCV.Formations).ToList<Formation>();

                        deletedFormations.ForEach(c => existingCV.Formations.Remove(c));

                            foreach (Formation f in addedFormations)
                    {
                        existingCV.Formations.Add(f);
                    }

                    }

formationsIds 是来自表单 (bunch of ) 的编队 id 的集合。这些是我希望我的简历具有的结构。

当 existingCV.Formation.Count() = 0 时它工作正常,但是当 existingCV.Formation.Count() > 0 时我得到一个 500 错误。换句话说,当我的 CV 已经在数据库中有一些编队时,代码给了我在线 500 错误var addedFormations = ...

请问如何解决这个问题?

于 2019-03-19T18:34:34.530 回答
0

如果您使用的是实体框架,那么删除集合中的所有元素就足够了。

实体框架中适当的多对多定义有两种方式:

class Formation
{
    public int Id {get; set;}
    ... // other columns

    // Every Formation has zero or more CVs (many-to-many)
    public virtual ICollection<CV> CVs {get; set;}
}
class CV
{
    public int Id {get; set;}
    ... // other columns

    // Every CV is for zero or more Formations (many-to-many)
    public virtual ICollection<Formation> Formations {get; set;}
}

virtual ICollections对于您的要求,在两侧都定义很重要。

给定一个formationId和一个cvId,用这些ID删除Formation和CV之间的关系,而不删除Formation和CV

var formationToClean = dbContext.Formations.Find(formationId);
var cvToClean = dbContext.CVs.Find(cvId);
// Consider fetching these items in one query.
// However, for this you will have to join three tables
// I'm not sure whether that will be faster then this Find that can use an index

formationToUpdate.CVs.Remove(cvToUpdate);
dbContext.SaveChanges();
于 2019-03-19T10:58:00.940 回答