locked
Value of type 'System.Data.Objects.ObjectContext' cannot be converted to 'System.Data.Entity.Core.Objects.ObjectContext'. RRS feed

  • Question

  • In Visual Studio 2012 on A windows 2008 R2 Server I just created an ASP.NET Dynamic Data Entities Website and added NuGet packages for Entity framework 6.0.2 and when I build the app, I had to change the references

    from:    

       Imports System.Data.Objects
       Imports System.Data.Objects.DataClasses



    to

      Imports System.Data.Entity.Core.Objects
      Imports System.Data.Entity.Core.Objects.DataClasses

    This eliminated all my errors except the 1... there's always one... grrrrr

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

    Here is the code that is the source of the error: on the "ManyToMany_Edit.ascx.vb" page:

        Private Sub ds_ContextCreated(ByVal sender As Object, ByVal e As EntityDataSourceContextCreatedEventArgs)
            ObjectContext = e.Context
        End Sub


    Here is the entire "ManyToMany_Edit.ascx.vb" page:

    Imports System.ComponentModel
    Imports System.ComponentModel.DataAnnotations
    Imports System.Web.DynamicData
    'Imports System.Data.Objects
    'Imports System.Data.Objects.DataClasses
    
    Imports System.Collections.Generic
    Imports System.Data.Entity.Core.Objects
    Imports System.Data.Entity.Core.Objects.DataClasses
    
    Class ManyToMany_EditField
        Inherits System.Web.DynamicData.FieldTemplateUserControl
        Protected Property ObjectContext() As ObjectContext
            Get
                Return m_ObjectContext
            End Get
            Set(value As ObjectContext)
                m_ObjectContext = value
            End Set
        End Property
        Private m_ObjectContext As ObjectContext
    
        Public Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            Dim ds As EntityDataSource = CType(Me.FindDataSourceControl, EntityDataSource)
    
            AddHandler ds.ContextCreated, AddressOf Me.ds_ContextCreated
    
            AddHandler ds.Updating, AddressOf Me.DataSource_UpdatingOrInserting
            AddHandler ds.Inserting, AddressOf Me.DataSource_UpdatingOrInserting
        End Sub
        Private Sub ds_ContextCreated(ByVal sender As Object, ByVal e As EntityDataSourceContextCreatedEventArgs)
            ObjectContext = e.Context
        End Sub
    
        Private Sub DataSource_UpdatingOrInserting(ByVal sender As Object, ByVal e As EntityDataSourceChangingEventArgs)
            Dim childTable As MetaTable = ChildrenColumn.ChildTable
            If (Mode = DataBoundControlMode.Edit) Then
                ObjectContext.LoadProperty(e.Entity, Column.Name)
            End If
    
            Dim entityCollection As Object = Column.EntityTypeProperty.GetValue(e.Entity, Nothing)
            For Each childEntity As Object In childTable.GetQuery(e.Context)
                Dim isCurrentlyInList As Object = ListContainsEntity(childTable, entityCollection, childEntity)
                Dim pkString As String = childTable.GetPrimaryKeyString(childEntity)
                Dim listItem As ListItem = CheckBoxList1.Items.FindByValue(pkString)
                If (listItem Is Nothing) Then
                    Continue For
                End If
                If listItem.Selected Then
                    If Not isCurrentlyInList Then
                        entityCollection.Add(childEntity)
                    End If
                ElseIf isCurrentlyInList Then
                    entityCollection.Remove(childEntity)
                End If
            Next
        End Sub
    
        Private Shared Function ListContainsEntity(table As MetaTable, list As IEnumerable(Of Object), entity As Object) As Boolean
            Return list.Any(Function(e) AreEntitiesEqual(table, e, entity))
        End Function
    
        Private Shared Function AreEntitiesEqual(table As MetaTable, entity1 As Object, entity2 As Object) As Boolean
            Return Enumerable.SequenceEqual(table.GetPrimaryKeyValues(entity1), table.GetPrimaryKeyValues(entity2))
        End Function
    
        Protected Sub CheckBoxList1_DataBound(sender As Object, e As EventArgs)
            Dim childTable As MetaTable = ChildrenColumn.ChildTable
    
            Dim entityCollection As IEnumerable(Of Object) = Nothing
    
            If Mode = DataBoundControlMode.Edit Then
                Dim entity As Object
                Dim rowDescriptor As ICustomTypeDescriptor = TryCast(Row, ICustomTypeDescriptor)
                If rowDescriptor IsNot Nothing Then
                    entity = rowDescriptor.GetPropertyOwner(Nothing)
                Else
                    entity = Row
                End If
    
                entityCollection = DirectCast(Column.EntityTypeProperty.GetValue(entity, Nothing), IEnumerable(Of Object))
                Dim realEntityCollection = TryCast(entityCollection, RelatedEnd)
                If realEntityCollection IsNot Nothing AndAlso Not realEntityCollection.IsLoaded Then
                    realEntityCollection.Load()
                End If
            End If
    
            For Each childEntity As Object In childTable.GetQuery(ObjectContext)
                Dim listItem As New ListItem(childTable.GetDisplayString(childEntity), childTable.GetPrimaryKeyString(childEntity))
    
                If Mode = DataBoundControlMode.Edit Then
                    listItem.Selected = ListContainsEntity(childTable, entityCollection, childEntity)
                End If
                CheckBoxList1.Items.Add(listItem)
            Next
        End Sub
    
        Public Overrides ReadOnly Property DataControl() As Control
            Get
                Return CheckBoxList1
            End Get
        End Property
        Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
            target = value
            Return value
        End Function
    End Class
    


    Please let me know what I am missing!!!

    Thanks,



    Dan Barry

    Monday, December 16, 2013 11:54 PM

Answers

  • Hello,

    EF 6 does not have System.Data.Objects.ObjectContext. EF 6 has moved some types, including

    ObjectContext, from System.Data.Entity.dll into EntityFramework.dll, and changed their namespaces.

    We can find that the ‘ObjectContext’ comes from the namespace System.Data.Entity.Core.Objects, ‘e.Context’ comes from System.Data.Objects namespace, actually their context are same and just in different namespace.

    For this issue, one way is to use the entity framework 5 so that the type will be same and it will work fine.

    I have also submitted this feedback to the MS team:

    https://connect.microsoft.com/VisualStudio/feedback/details/811743/bug-or-by-designed-in-dynamic-entities-web-application-with-entity-framework-6

    As soon as they have any update regarding this issue, I will tell you.

    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.

    • Marked as answer by Fred Bao Wednesday, December 25, 2013 9:03 AM
    Tuesday, December 17, 2013 3:06 AM

All replies

  • Hello,

    EF 6 does not have System.Data.Objects.ObjectContext. EF 6 has moved some types, including

    ObjectContext, from System.Data.Entity.dll into EntityFramework.dll, and changed their namespaces.

    We can find that the ‘ObjectContext’ comes from the namespace System.Data.Entity.Core.Objects, ‘e.Context’ comes from System.Data.Objects namespace, actually their context are same and just in different namespace.

    For this issue, one way is to use the entity framework 5 so that the type will be same and it will work fine.

    I have also submitted this feedback to the MS team:

    https://connect.microsoft.com/VisualStudio/feedback/details/811743/bug-or-by-designed-in-dynamic-entities-web-application-with-entity-framework-6

    As soon as they have any update regarding this issue, I will tell you.

    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.

    • Marked as answer by Fred Bao Wednesday, December 25, 2013 9:03 AM
    Tuesday, December 17, 2013 3:06 AM
  • Hello,

    The team is woring on with Dynamic Data and EF6.

    It requires the team to build a new EDSC that works with EF6, this work is tracked here -

    https://entityframework.codeplex.com/workitem/1515.

    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.

    Tuesday, December 31, 2013 9:37 AM
  • Resolved by MS on Feb'2014. Just install on PM:

    Install-Package Microsoft.AspNet.EntityDataSource

    http://www.nuget.org/packages/Microsoft.AspNet.EntityDataSource/

    And replace:

        Private Sub ds_ContextCreated(ByVal sender As Object, ByVal e As EntityDataSourceContextCreatedEventArgs)
            ObjectContext = e.Context
        End Sub

    by:

    Private Sub ds_ContextCreated(ByVal sender As Object, ByVal e As Microsoft.AspNet.EntityDataSource.EntityDataSourceContextCreatingEventArgs)

    ObjectContext = e.Context End Sub

    Wednesday, April 23, 2014 9:57 AM
  • I have installed the Microsoft.AspNet.EntityDataSource package, but I can't seem to figure out where to replace the EntityDataSourceContextCreatedEventArgs with Microsoft.AspNet.EntityDataSource.EntityDataSourceContextCreatingEventArgs.

    I am still getting the same error - 'System.Data.Objects.ObjectContext' cannot be converted to 'System.Data.Entity.Core.Objects.ObjectContext' even after the install.

    Help is appreciated!

    Tuesday, May 6, 2014 11:19 PM
  • Did you manage to get this working? 

    I have the same problem, but the Microsoft.AspNet.EntityDataSource.EntityDataSourceContextCreatingEventArgs does not match the method signature in EntityDataSource OnContextCreating. 
    Thursday, May 28, 2015 1:13 PM