2

Im setting up an OData provider in visual studio. The error that im receiving really doesnt have anything to do with OData side of things.

I have a table type in my ado entity data model and whenever I try to insert a record into this table i get the following error:

{"The member with identity 'ReturnValue' does not exist in the metadata collection. Parameter name: identity"}

This is the stacktrace:

at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache) at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options) at System.Data.Objects.ObjectContext.SaveChanges() at OData.CreateWorkOrder(Int32 CreatedByContactID) in D:\Web\OData.svc.vb:line 31

Has anyone heard of this error? I can insert fine into any other table it just seems to be this one table that the ado entity data model doesnt want to play with.

Thanks in advance

''# this comment is just here because the code formatter doesn't play nice otherwise.
<WebGet()> _
Public Function CreateWorkOrder(ByVal CreatedByContactID As Integer) As WorkOrder
    Dim x As New MyAppEntities

    Dim wo As WorkOrder = MyApp.WorkOrder.CreateWorkOrder(Nothing, 100, 4, False, DateTime.Now, False, 0, 1, 0, 0, 0, 0, 0, 0, False, 0, 0, 0, False, CreatedByContactID, DateTime.Now, 1, DateTime.Now)
    x.AddToWorkOrders(wo)
    x.SaveChanges()
    Return wo
End Function
Chase Florell
  • 46,378
  • 57
  • 186
  • 376
MattyD
  • 185
  • 1
  • 3
  • 14
  • Code please (show us the content of OData.svc.vb in and around line 31). How are you submitting the data to the database. It looks like you're trying to submit "ReturnValue" when the field doesn't exist in the DB. – Chase Florell Nov 28 '10 at 21:35
  • _ Public Function CreateWorkOrder(ByVal CreatedByContactID As Integer) As WorkOrder Dim x As New MyAppEntities Dim wo As WorkOrder = MyApp.WorkOrder.CreateWorkOrder(Nothing, 100, 4, False, DateTime.Now, False, 0, 1, 0, 0, 0, 0, 0, 0, False, 0, 0, 0, False, CreatedByContactID, DateTime.Now, 1, DateTime.Now) x.AddToWorkOrders(wo) x.SaveChanges() Return wo End Function – MattyD Nov 28 '10 at 21:41
  • If you edit your original question, you can take advantage of the code formatting in the Markdown Editor. I can't really read that. – Chase Florell Nov 28 '10 at 21:50
  • I'm not going to enter an "answer" as this is more of a suggestion to try and make things easier. You could first create a new workorder `Dim workOrder As New WorkOrder` then `With WorkOrder ... End With`. Set all of your parameters in there. From there, on your `CreateWorkOrder` method, why not ask for a single parameter `CreateWorkOrder(workOrder)`. – Chase Florell Nov 28 '10 at 22:01
  • 1
    It ended up being an insert trigger on the table that the entity framework did not like. – MattyD Nov 28 '10 at 22:36
  • are you using stored procedures for simple inserts? Entity Framework is really cool when you start submitting object rather than individual parameters. Makes life way more fun. – Chase Florell Nov 28 '10 at 22:37

2 Answers2

0

Identity Key is Case Sensitive. Pls check is it properly provided as Parameter. Example:

context.mCustomersReference.EntityKey = new EntityKey("BulkEntities.CustomerSet", "CustomerId", Convert.ToInt64(id));
Foggzie
  • 9,691
  • 1
  • 31
  • 48
revan
  • 121
  • 1
  • 2
0

I'm assuming that this is the erroring line

Dim wo As WorkOrder = MyApp.WorkOrder.CreateWorkOrder(Nothing, 100, 4, False, DateTime.Now, False, 0, 1, 0, 0, 0, 0, 0, 0, False, 0, 0, 0, False, CreatedByContactID, DateTime.Now, 1, DateTime.Now)

Now the first parameter is "Nothing" whereby I'm assuming it's supposed to the the "Return Value" of a stored procedure (are you using stored procedures?).

If you are in fact using a stored procedure with something like @ID int output, then you need a proper output parameter in your code

        Dim IDOut As Integer
        Dim wo As WorkOrder = MyApp.WorkOrder.CreateWorkOrder(IDOut, 100, 4, False, DateTime.Now, False, 0, 1, 0, 0, 0, 0, 0, 0, False, 0, 0, 0, False, CreatedByContactID, DateTime.Now, 1, DateTime.Now)

        ''# Do crap with IDOut like redirect to http://example.com?workorder=[IDOut]

But as I said in my comment, you'd be better of using LINQ to SQL and avoiding Stored Procedures for simple inserts.

Something along the lines of

    Public Sub GenerateWorkOrder()

        Dim workOrder As New WorkOrder
        With workOrder
             .somestringParameter1 = "the parameter"
             .someintegerParameter1 = 5
             ''# etc
        End With

        CreateWorkOrder(workOrder)
        SubmitChanges()

    End Sub


    Public Sub CreateWorkOrder(ByVal workOrder As WorkOrder)
        ''# dc is simply the DataContext
        dc.WorkOrders.InsertOnSubmit(workOrder)
    End Sub

    Public Sub SubmitChanges()
        dc.SubmitChanges()
    End Sub
Chase Florell
  • 46,378
  • 57
  • 186
  • 376