1 Replies - 374 Views - Last Post: 19 November 2012 - 10:03 AM Rate Topic: -----

#1 AlwaysLearning  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 02-November 12

WPF entity Framework return ID of added child record

Posted 19 November 2012 - 07:18 AM

Hi guys,

Basically i am trying to return the ID of the added child record. My current code is....

CounterfeitEntities db = new CounterfeitEntities();

                int rowcount = Convert.ToInt32((flx_counterfeitParts[flx_counterfeitParts.SelectedIndex, 0]));

                var countpart = db.CounterfeitParts.First(c => c.CounterfeitID == rowcount);
                countpart.CounterfeitPartsImages.Add(new CounterfeitPartsImage
                {
                    CountImageComment = txtCommentInput.Text,
                    CountImageURL = txtImageLinkInput.Text
                });

                db.SaveChanges();



I have then tried various ways of getting the ID of the added child such as...

string anything = countpart.ChildID
string anything = countpart.CounterfeitPartsImage.ChildID

Any help would be appreciated.

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: WPF entity Framework return ID of added child record

#2 MathewS  Icon User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 342
  • Joined: 14-May 02

Re: WPF entity Framework return ID of added child record

Posted 19 November 2012 - 10:03 AM

You can do ID on the Added entry.

You just need to create the object before and do the .add method, save, then you can use id.

CounterfeitEntities db = new CounterfeitEntities();

int rowcount = Convert.ToInt32((flx_counterfeitParts[flx_counterfeitParts.SelectedIndex, 0]));

var countpart = db.CounterfeitParts.First(c => c.CounterfeitID == rowcount);
CounterfeitPartsImage image = new CounterfeitPartsImage
                {
                    CountImageComment = txtCommentInput.Text,
                    CountImageURL = txtImageLinkInput.Text
                }

countpart.CounterfeitPartsImages.Add(image);

db.SaveChanges();

int id = image.Id;



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1