What should you do?

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?

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.
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.

B.
Add a try/catch statement around every call to the SaveChanges() method.

C.
Remove the unique constraint on the Name column in the Colors table.

D.
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



Leave a Reply 2

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


John Galt

John Galt

Why is the answer A? I just googled LoadOrCreate and can’t find anything on it. I’m guessing this is a function that’s custom created by user for this question?

John Galt

John Galt

Apparently this question is part of the case study that’s not here. I saw the case study, and the answer is correct.