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?

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?

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)



Leave a Reply 3

Your email address will not be published. Required fields are marked *


Khoa Nguyen

Khoa Nguyen

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.

John Galt

John Galt

@Khoa – normally I would agree, but there are plenty of examples of Microsoft not taking entity state into account. Look at previous question #172 for example.

John Galt

John Galt

EDIT: Since some brilliant fucking mod decided to flip the questions, the question I refered to above is no longer #172, it’s now #17