将 Identity 值从 Entityframework 映射到 DTO

Mapping Identity value into DTO from Entityframework

提问人:Eldho 提问时间:5/18/2015 更新时间:6/28/2015 访问量:637

问:

我有方法可以从我的 DTOCollection 进行批量插入,它有 3 级循环。

public List<FirstCollectionDTO> BulkInsert(List<FirstCollectionDTO> collection)
    {
        using (var db = new DemoEntities())
        {
            //Setting Auto Detech Changes false when starting loop
            db.Configuration.AutoDetectChangesEnabled = false;

            //Looping start
            collection.ForEach(myitem =>
                {
                    myitem.SecondCollection.ForEach(secondLevel =>
                        {
                                 //Some Operation Over here


                            secondLevel.ThirdCollection.ForEach(thirdItem =>
                                {
                                    var myEntity= new MyTable()
                                    {
                                        Level=thirdItem.Name,
                                        Method=thirdItem.Method,
                                      Representation=thirdItem.Representation,
                                    };

                                    var insertedItem = db.MyTable.Add(myEntity);

                                    //Setting the reference of id to the 
                                    //DTO after the save changes. 
                                    //This seems always 0 as it this time
                                    // it have no id only after save changes the id be passed into InsertedItem , I have to pass the reference to myDTO also
                                    thirdItem.Id = insertedItem.Id;
                                });
                        });
                });

            //Setting Auto Detech Changes true when starting loop
            db.Configuration.AutoDetectChangesEnabled = true;

            // Here I save my changes
            db.SaveChanges();

            return collection;
        }
    }

如果 INSERT 操作成功,则写回服务器生成的值。那么我如何将这个值也纳入我的 DTO。

保存更改后如何获取插入对象的 Id,即使

C# 实体框架 ORM 按值传递

评论

1赞 Gert Arnold 5/18/2015
这些值在 期间被赋值并读回实体,因此在此之前您无法知道它们。SaveChanges
0赞 Eldho 5/18/2015
@GertArnold有什么方法可以在保存更改后获得 id 并保持 3 个级别的关系。
0赞 Gert Arnold 5/18/2015
thirdItem可以有一个属性,包含实体对象,因此可以返回实体的 Id。MyTablethirdItem.Id
0赞 Eldho 5/18/2015
@GertArnold,是 DTO 对象,我宁愿避免来自 DTO 的 EF 引用ThirdItem
1赞 Gert Arnold 5/18/2015
然后,在此过程中使用接口(用于 EF 类)或生成 DTO/实体对的字典。

答:

0赞 Eldho 6/28/2015 #1
//To get the inserted id 
var anonymousprojectList = (new[] { new { FirstCollectionId= 0, SecondCollectionId= 0, ThirdObject= new Document() } }).ToList();


using (var db = new DemoEntities())
    {
        //Setting Auto Detech Changes false when starting loop
        db.Configuration.AutoDetectChangesEnabled = false;

        //Looping start
        collection.ForEach(myitem =>
            {
                myitem.SecondCollection.ForEach(secondLevel =>
                    {
                             //Some Operation Over here


                        secondLevel.ThirdCollection.ForEach(thirdItem =>
                            {
                                var myEntity= new MyTable()
                                {
                                    Level=thirdItem.Name,
                                    Method=thirdItem.Method,
                                  Representation=thirdItem.Representation,
                                };

                                var insertedItem = db.MyTable.Add(myEntity);

                                //Setting the reference of id to the 
                                //DTO after the save changes. 
                                //This seems always 0 as it this time
                                // it have no id only after save changes the id be passed into InsertedItem , I have to pass the reference to myDTO also
                                using (var db = new DemoEntities())
    {
        //Setting Auto Detech Changes false when starting loop
        db.Configuration.AutoDetectChangesEnabled = false;

        //Looping start
        collection.ForEach(myitem =>
            {
                myitem.SecondCollection.ForEach(secondLevel =>
                    {
                             //Some Operation Over here


                        secondLevel.ThirdCollection.ForEach(thirdItem =>
                            {
                                var myEntity= new MyTable()
                                {
                                    Level=thirdItem.Name,
                                    Method=thirdItem.Method,
                                  Representation=thirdItem.Representation,
                                };

                                var insertedItem = db.MyTable.Add(myEntity);

                                //Setting the reference of id to the 
                                //DTO after the save changes. 
                                //This seems always 0 as it this time
                                // it have no id only after save changes the id be passed into InsertedItem , I have to pass the reference to myDTO also
                                anonymousprojectList.Add(new {FirstCollectionID = myItem.Od,SecondCollectionId=secondLevel.Id,ThirdObject = insertedItem});
                            });
                    });
            });

        //Setting Auto Detech Changes true when starting loop
        db.Configuration.AutoDetectChangesEnabled = true;

        // Here I save my changes
        db.SaveChanges();

                            });
                    });
            });

        //Setting Auto Detech Changes true when starting loop
        db.Configuration.AutoDetectChangesEnabled = true;

        // Here I save my changes
        db.SaveChanges();
   }
       // Loop through the anonymous collection and assign to the input collection itself 
       //Skip header item
       anonymousprojectList.Skip(1).ToList().ForEach(object=>
        {
          //Loop through the three level and assign the value

          //Assign the value
          ThirdlevelItem.Id = ThirdObject.Id) 

        });

return collection