PrepAway - Latest Free Exam Questions & Answers

Which code segment should you insert at line 06?

You use Microsoft .NET Framework 4.0 to develop an application that uses WCF Data Services to persist entities from the following Entity Data Model.

You create a new Blog instance named newBlog and a new Post instance named newPost as shown in the following code segment.
(Line numbers are included for reference only.)

01 Blog newBlog = new Blog();
02 Post newPost = new Post();
03 ….
04 Uri serviceUri = new Uri(“”);
05 BlogsEntities context = new BlogsEntities(serviceUri);
06 ….

You need to ensure that newPost is related to newBlog through the Posts collection property and that newPost and newBlog are sent to the service.
Which code segment should you insert at line 06?

PrepAway - Latest Free Exam Questions & Answers

A.
context.AttachLink(newBlog, “Posts”, newPost);
context.SaveChanges(SaveChangesOptions.Batch) ;

B.
newBlog.Posts.Add(newPost);
context.AddToBlogs(newBlog);
context.AddToPosts(newPost);
context.SaveChanges(SaveChangesOptions.Batch);

C.
newBlog.Posts.Add(newPost);
context.AttachTo(“Blogs”, newBlog);
context.AttachTo(“Posts”, newPost);
context.SaveChanges(SaveChangesOptions.Batch);

D.
newBlog.Posts.Add(newPost);
context.UpdateObject(newBlog);
context.UpdateObject(newPost);
context.SaveChanges(SaveChangesOptions.Batch);

Explanation:
Attaching and Detaching objects
(http://msdn.microsoft.com/en-us/library/bb896271.aspx)

3 Comments on “Which code segment should you insert at line 06?

  1. Khoa Nguyen says:

    This answer is not correct.
    AttachTo leaves the entity state as Unchanged, therefore when calling SaveChanges the new entities are not persisted to the database. I tried the code and it doesn’t work.

    On the other hand, Answer B works though AddToXYZ methods are deprecated.




    0



    0

Leave a Reply