简体   繁体   English

实体框架:更新实体与同类型对象集合的关系

[英]Entity Framework: Update entity with relationship to collection of objects of same type

I would like to know if the following is possible with fewer database calls. 我想知道以下使用较少的数据库调用是否有可能。

I have a model where I need to go back and save other entities of the same type to itself (obviously avoiding circular referencing). 我有一个模型,我需要回去并将其他相同类型的实体保存到自身(显然避免循环引用)。

public class OneItemOfTheModel
{
    [Key]
    [Display(Name = "PK Id")]
    public int OneItemOfTheModelPK{ get; set; }

    [Display(Name = "Listing ID")]
    public int ListingID{ get; set; }

    [Display(Name = "Cool Property name")]
    public string CoolProp{ get; set; }

    [Display(Name = "Related Models")]
    public virtual ICollection<OneItemOfTheModel> RelatedModels { get; set; }
    [Display(Name = "Parent Object Thing")]
    public virtual ParentObjectThing ParentObj { get; set; }
 }

Data is imported from rows of a csv file. 数据是从csv文件的行中导入的。 Initially a "Parent Object Thing" will be created, a new model entity created and linked to that parent and fields populated with the row in the csv: 最初,将创建一个“父对象事物”,创建一个新的模型实体并将其链接到该父对象以及用csv中的行填充的字段:

 ListingID/CoolProp
 1, "whoa"
 2, "awesome"
 3, "data"
 4, "awesome"
 5, "awesome"

The desired result is to end up with the following where entities with the same "Cool Property" are linked in the database: 所需的结果是在数据库中链接具有相同“酷属性”的实体时,得到以下结果:

 PK/ListingID/CoolProp/RelatedModels/ParentObj
 5083,1,"whoa",,53
 5084,2,"awesome",[5086,5087],53
 5085,3,"data",,53
 5086,4,"awesome",[5084,5087],53
 5087,5,"awesome",[5084,5086],53

Currently I am saving entities to the database first and then running another followup method to link individual entities together. 目前,我先将实体保存到数据库,然后运行另一种后续方法将各个实体链接在一起。

 public void SyncRelatedProperties (int parentObjID, efContext db )
    {
        List<OneItemOfTheModel> myItemList = db.OneItemOfTheModel.Where(b => b.ParentObjID == ParentObjID && b.CoolProp.Length > 0).ToList();
        if (myItemList.Count() > 0)
        {
            foreach( OneItemOfTheModel t in myItemList)
            {
                List<OneItemOfTheModel> tmplist = db.OneItemOfTheModel.Where(b => b.CoolProp == t.CoolProp && b.ListingID != t.ListingID).ToList();
                foreach ( OneItemOfTheModel b in tmplist)
                {
                    t.RelatedModels.Add(b);
                }
            }
        db.SaveChanges();
        }

Obviously this doesn't scale very well when users are importing files with hundreds of lines with tens of related properties to map. 显然,当用户导入具有数百行且具有数十个相关属性的文件以进行映射时,这种方法无法很好地扩展。

I've played around a bit with "GroupBy" on CoolProp and clearing out the self referencing ListingID but I'm not confident enough with my knowledge of EF to know at what stage I'll be referencing a shallow copy instead of the original entity. 我在CoolProp上使用“ GroupBy”进行了一些操作,并清除了自我引用的ListingID,但是我对EF的了解不够自信,无法知道在哪个阶段我将引用浅表副本而不是原始实体。

Have you tried just adding all the Entities and calling .SaveChanges()? 您是否尝试过仅添加所有实体并调用.SaveChanges()? EF will attempt a Topological Sort on the entities so that they are inserted in an order that is consistent with the foreign key dependencies. EF将尝试对实体进行拓扑排序 ,以便按照与外键依赖项一致的顺序插入实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM