0

I am experimenting with a Web API 2 project in Visual Studio 2012. I used the code first from existing DB option with EF6 to select one table and one view. I then tried to create a controller for the simple table using the profile for Web API 2 OData. The scaffolding of the controller fails telling me that "the item with identity 'Client Last Reveiwed On' already exists in the metadata collection". The problem is not only am I sure that field is unique for this project but that field is part of the view and not the table. Below is the model generated for the simple table (t_Client) that I was trying to create the controller for. As you can see the offending column is not part of the class. I will add below the definition for the column that VS/EF doesn't like which is in the class for the view.

Any ideas why this won't work?

Partial Public Class t_Client
<Key>
<DatabaseGenerated(DatabaseGeneratedOption.None)>
Public Property ClientID As Integer

<Required>
<StringLength(255)>
Public Property ClientName As String

Public Property isActive As Boolean

End Class

Here is the column that is defined in a separate view.

<Column("Client Last Reviewed On", TypeName:="date")>
Public Property Client_Last_Reviewed_On As Date?
pretzelb
  • 1,131
  • 2
  • 16
  • 38

1 Answers1

0

I am not sure which of these steps fixed the issue but here are some notes on the topic.

  1. Removing references to the model based on the SQL view eliminated errors.
  2. I went into SQL and updated the view to contain a row number column.
  3. Even with the row number column, EF tagged multiple columns as the key.
  4. I manually edited the model to make the row number column the key.
  5. I also had to update a cast the data type of a few columns in the SQL view to match reality, mainly bigint that was really just integer.

My guess is the fix was the well defined key.

pretzelb
  • 1,131
  • 2
  • 16
  • 38