Microsoft TS: Accessing Data with Microsoft .NET Framework 4 Sample Questions:
1. You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4.0 to develop an application.
You use the ADO.NET Entity Framework Designer to model entities.
You need to associate a previously deserialized entity named person1 to an object context named model
and persist changes to the database.
Which code segment should you use?
A) model.AttachTo("People", person1); model.SaveChanges();
B) person1.AcceptChanges(); model.SaveChanges();
C) model.People.Attach(person1); model.SaveChanges();
D) model.People.ApplyChanges(person1) ; model.SaveChanges();
2. You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4.0 to create an application.
The application connects to a Microsoft SQL Server database. The application uses the ADO.NET Entity
Framework to model entities.
The database includes objects based on the exhibit (click the Exhibit button).
The application includes the following code segment. (Line numbers are included for reference only.)
01 using(AdventureWorksEntities context = new AdventureWorksEntities())
02 {
03 ...
04 foreach (SalesOrderHeader order in customer.SalesOrderHeader)
05 {
06 Console.WriteLine(String.Format("Order: {0} ",
order.SalesOrderNumber));
07 foreach (SalesOrderDetail item in order.SalesOrderDetail)
08 {
09 Console.WriteLine(String.Format("Quantity: {0} ", item.Quantity));
10 Console.WriteLine(String.Format("Product: {0} ",
item.Product.Name));
11 }
12 }
13 }
You want to list all the orders for a specific customer. You need to ensure that the list contains following fields:
-Order number
-Quantity of products
-Product name
Which code segment should you insert in line 03?
A) Contact customer = context.Contact.Where("it.ContactID = @customerId", new ObjectParameter("customerId", customerId)).First();
B) Contact customer = (from contact in context.Contact.Include
("SalesOrderHeader.SalesOrderDetail")
select contact).FirstOrDefault();
C) Contact customer = (from contact in context.Contact.Include
("SalesOrderHeader")
select contact).FirstOrDefault();
D) Contact customer = context.Contact.Where("it.ContactID = @customerId", new ObjectParameter("@customerId", customerId)).First();
3. You have an existing ContosoEntities context object named context.
Calling the SaveChanges() method on the context object generates an exception that has the following inner exception:
System.Data.SqlClient.SqlException: Cannot insert duplicate key row in object 'dbo.Colors' with unique index 'IX_Colors'.
You need to ensure that a call to SaveChanges() on the context object does not generate this exception. What should you do?
A) Override the SaveChanges() method on the ContosoEntities class, call the ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Added) method, and call the AcceptChanges() method on each ObjectStateEntry object it returns
B) Add a try/catch statement around every call to the SaveChanges() method.
C) Examine the code to see how Color objects are allocated. Replace any instance of the new Color() method with a call to the ContosoEntities.LoadOrCreate() method.
D) Remove the unique constraint on the Name column in the Colors table.
4. You develop a Microsoft .NET application that uses Entity Framework to store entities in a Microsft SQL
Server 2008 database.
While the application is disconnected from the database, entities that are modified, are serialized to a local
file.
The next time the application connects to the database, it retrieves the identity from the database by using
an object context
named context and stores the entity in a variable named remoteCustomer.
The application then serializes the Customer entity from the local file and stores the entity in a variable
named localCustomer.
The remoteCustomer and the localCustomer variables have the same entity key.
You need to ensure that the offline changes to the Customer entity is persisted in the database when the
ObjectContext.SaveChanges() method is called.
Which line of code should you use?
A) context.ApplyCurrentValues("Customers", localCustomer);
B) context.ApplyOriginalValues("Customers", remoteCustomer);
C) context.ApplyCurrentValues("Customers", remoteCustomer);
D) context.ApplyOriginalValues("Customers", localCustomer);
5. You use Microsoft .NET Framework 4.0 to develop an application that connects to a Microsoft SQL Server
2008 database.
The application includes a table adapter named taStore, which has the following DataTable.
There is a row in the database that has a ProductID of 680. You need to change the Name column in the
row to "New Product Name".
Which code segment should you use?
A) var dt = new taStore.ProductDataTable(); var row = dt.NewProductRow(); row.ProductID = 680; row.Name = "New Product Name"; dt.Rows.Add(row) ;
B) var dt = new taStore.ProductDataTable(); var ta = new taStoreTableAdapters.ProductTableAdapter(); ta.Fill(dt); var dv = new DataView(); dv.RowFilter = "680"; dv[0]["Name"] = "New Product Name"; ta.Update(dt);
C) var dt = new taStore.ProductDataTable(); var ta = new taStoreTableAdapters.ProductTableAdapter(); ta.Fill(dt); taStore.ProductRow row = (taStore.ProductRow)dt.Rows.Find(680) ; row.Name = "New Product Name"; ta.Update(row);
D) var ta = new taStoreTableAdapters.ProductTableAdapter(); var dt = ta.GetData(); var row = dt.Select("680") ; row[0]["Name"] = "New Product Name"; ta.Update(row);
Solutions:
| Question # 1 Answer: A | Question # 2 Answer: A | Question # 3 Answer: C | Question # 4 Answer: A | Question # 5 Answer: C |
We're so confident of our products that we provide no hassle product exchange.


By Jay

