none
Problem with entity passed as argument RRS feed

  • Question

  • Hello !

    I have this code :

    Public Class myclass
        ....
        Private cont As MyentEntities
        .....
        .....
    
        Public Sub New( ByRef cnx As MyentEntities)
           .....
            cont = cnx
            .....
        End Sub
    
    Public Sub test
    .....
    
    If cont.mytable1.Count=1 then
    
    .....
    end if 
    
    End Sub
    End class
    
    
    'On my main form :
    
    .......
    Dim E1 as MyEntEntities
    Dim m1 as myclass
    Private Sub MainForm_Load(sender As System.Object, e As System.EventArgs) Handles Me.Load 
    
    E1=New MyentEntities
    m1=new MyClass(E1)
    ........
    
    End Sub
    
    Private sub fill_entity
    
    E1.MyTable1.Tolist
    ....
    End sub
    
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    
    m1.test
    
    End Sub

    When I click the button , I get an error :

    ....the entity of type 'MyTable1'  does not exist in the context.

    But I passed E1 with ByRef ?

    What's wrong ?

    Thank you !

    Thursday, January 22, 2015 8:33 PM

All replies

  • Hello,

    >>Problem with entity passed as argument

    With your provided code, I tested with my database, it is ok, below is the test demo which is similar with yours except the used databse:

    Public Class Form1
        Dim E1 As DFDBEntities
        Dim m1 As TestClass
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            E1 = New DFDBEntities
            m1 = New TestClass(E1)
        End Sub
        Private Sub fill_entity()
            E1.Orders.Tolist()
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            m1.test()
        End Sub
    End Class
    Public Class TestClass
        Private cont As DFDBEntities
    
        Public Sub New(ByRef cnx As DFDBEntities)
            cont = cnx
        End Sub
    
        Public Sub test()
    
            If cont.Orders.Count = 1 Then
            End If
        End Sub
    End Class
    

    Since we do not know your exact table structure, it is hard to know why this exception is thrown in your side, one reason I know is the .edmx file is not mapped correctly, you could check this link which describes various possibilities for the caused reason of this exception:

    http://stackoverflow.com/questions/5634392/ef-4-1-code-first-error-the-entity-type-sometype-is-not-part-of-the-model-for

    Or you could provide information as what database you are using, the table you are working with and the Entity Framework version.

    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.

    Friday, January 23, 2015 5:52 AM
    Moderator
  • Hello !

    I want to clarify an important thing : In my code I forget to illustrate where I call the Fill_entity sub. It's called after the initialization of m1 variable.So it's like this :

    Private Sub MainForm_Load(sender As System.Object, e As System.EventArgs) Handles Me.Load 
    
    E1=New MyentEntities
    m1=new MyClass(E1)
    Fill_entity()
    End Sub

    Do your code works with this change ?

    Thank you !

    Friday, January 23, 2015 3:39 PM
  • Hello,

    After add this method:

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            E1 = New DFDBEntities
    
            m1 = New TestClass(E1)
    
            fill_entity()
    
        End Sub
    

    It could still work. I suggest that you could have a try to recreate the model to see if it works.

    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.

    Monday, January 26, 2015 9:56 AM
    Moderator