none
Why i am getting errors like "Value of type 'System.Data.Objects.ObjectParameter' cannot be converted to 'System.Data.Entity.Core.Objects.ObjectParameter'?

    Question

  • Good afternoon All,

    This is what is my scenario

    Background:-

    (1)  We have a ASP.NET website (not Web application project) which started long back using LINQ to SQL (.dbml) model.

    (2)  We have pages been developed and dependent on views, stored procedures and tables

    (3)  Recently it's been decided let's use Model Binding features of ASP.NET 4.5 framework.

    (4)  So i been asked to investigate and i tried all options and the good one i found is "ADO.NET Data Entity Model" as it gives designer and i can update my model from Database. as many time as we want. (Since we have existing database and which changes time to time we wanted to use this Database First model approach).

    NOTE:- We are using VS.NET Professional 2012 Update 3 and SQL Server 2008 R2

     What I Did?

    (1)  I created a copy of my existing ASP.NET website and inside that I installed the EF 6.1

    (2)   I gone to my NuGet Packages Manager and added installed EF 6.1.

    (3)  I created a Models folder under “App_Code” folder of our WebSite and by selecting this Model folder (App_Code è Model) I added the template “ADO.NET Data Entity Model” and selected my existing database and it created the needed POCO’s in a single file (Ex:- In my case Model.vb file). Since the Sql Server views can not take parameters, i created few stored  procedures and added them in .edmx designer. When i do that i get below type of errors.

    Value of type 'System.Data.Objects.ObjectParameter' cannot be converted to 'System.Data.Entity.Core.Objects.ObjectParameter'

    I saw below link and i created a seperate class under Model folder and named it same as My .edmx Context class and made it partial

    Ex:-

    (1) MyProjectNameModel.Context.tt

    (2) MyProjectNameModel.Context.vb (Assume the name of class inside this physical file is "MyComp_MyProjectEntities"

    (3) MyProjectNameModel.edmx

    (4) MyProjectNameModel.Designer.vb

    (5) MyProjectNameModel.edmx.diagram

    (6) MyProjectNameModel.tt

    (7) MyProjectNameModel.vb

    I create one more partial class with the same name as item (2) mentioned above in my Model folder and moved all functions code to this file and also i commented all the functions created of (adding stored procedures in .edmx) in the original .edmx file. In the above scenario "MyProjectNameModel.Context.vb" of line item (2)

    For Ex:-

    Assume i am adding a stored procedure in .edmx designer and for example it is "usp_GetThirtyDayData".

    MyComp_MyProjectEntities" ( with physical file name ==> MyProjectNameModel.Context.vb )

    has code generated like below

    '------------------------------------------------------------------------------

    ' <auto-generated>

    '    This code was generated from a template.

    '

    '    Manual changes to this file may cause unexpected behavior in your application.

    '    Manual changes to this file will be overwritten if the code is regenerated.

    ' </auto-generated>

    '------------------------------------------------------------------------------

    Imports System

    Imports System.Data.Entity

    Imports System.Data.Entity.Infrastructure

    Imports System.Data.Objects

    Imports System.Data.Objects.DataClasses

    Imports System.Linq

    Namespace Models

        Partial Public Class MyProjectNameModel

            Inherits DbContext

                Public Sub New()

                MyBase.New("name=MyProjectDBConnectionString")

            End Sub

            Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)

                Throw New UnintentionalCodeFirstException()

            End Sub

            Public Property histories() As DbSet(Of history)

                'Public Overridable Function usp_GetThirtyDayData() As ObjectResult(Of usp_GetThirtyDayData_Result)

            '    Return DirectCast(Me, IObjectContextAdapter).ObjectContext.ExecuteFunction(Of usp_GetThirtyDayData_Result)("usp_GetThirtyDayData")

            'End Function

        End Class

    End Namespace

    My partial class (MyComp_MyProjectEntities.vb) which i seperately created has below code

    Imports System

    Imports System.Data.Entity

    Imports System.Data.Entity.Core.Objects

    Imports System.Data.Entity.Infrastructure

    Imports System.Linq

    Namespace Models

        Partial Public Class MyProjectNameModel

            Inherits DbContext

            Public Overridable Function usp_GetThirtyDayData() As ObjectResult(Of usp_GetThirtyDayData_Result)

                Return DirectCast(Me, IObjectContextAdapter).ObjectContext.ExecuteFunction(Of usp_GetThirtyDayData_Result)

                 ("usp_GetThirtyDayData")

            End Function

        End Class

    End Namespace

    Note:-

    As you see i commented the function "usp_GetThirtyDayData" in the original context class "MyProjectNameModel" and

    added this in my seperate partial class with the same name "MyProjectNameModel", then it is alright. My question is that is there a way that i don't need to do this, means is there an alernate so that i don't need to create a seperate partial class "MyProjectNameModel" and move my functions there (which are generated of adding stored procedures)?

    As you see these functions needs "using System.Data.Objects;" and "using System.Data.Entity.Core.Objects;" namespaces at the top.

    http://stackoverflow.com/questions/19661494/entity-framework-6-cannot-build-after-adding-stored-procedures-to-data-model


    • Edited by ThotaAshok Tuesday, December 31, 2013 8:43 PM Corrections
    Tuesday, December 31, 2013 8:41 PM

Answers

  • Hello,

    >>My question is that is there a way that i don't need to do this, means is there an alernate so that i don't need to create a seperate partial class "MyProjectNameModel" and move my functions there (which are generated of adding stored procedures)?

    I think the solution is already in the link provided by you, for this, we just need to change the namespace System.Data.Objects to System.Data.Entity.Core.Objects and then we do not need to add the partial.

    Or we can modify the XXX.context.tt file, since you are using VB, we need to change System.Data.Objects to System.Data.Entity.Core.Objects in line 35. After that, the context.vb then will always import the System.Data.Entity.Core.Objects namespace.

    If I misunderstand, please let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, January 01, 2014 1:59 AM
    Moderator