1

Hi I'm try to use EF with a code first workflow against an existing database but I am getting the error:

{"Value cannot be null. Parameter name: source"}

when using the following line:

    Dim ctx = New MVCTest1
    Dim t = ctx.Companies.ToList()

My POCO classes are included below:

I have a standard conn string called MVCTest1 which points at SQL Server. I think EF is using that as I can see it in connection string of the dbContext. Does the conn string need to be in a special format. I also noticed the onmodelcreated isn't being called. I'm not sure if that's relevant. Very new to this so I suspect it's something obvious Cheers, Chris

Imports System.Data.Entity

Public Class Company
    Public CompanyID As Integer
    Public CompanyName As String
    Public Overridable Property Contacts As ICollection(Of Contact)
End Class

Public Class Contact
    Public ContactID As Integer
    Public FirstName As String
    Public LastName As String
    Public Overridable Property Company As Company
End Class

Public Class MVCTest1
    Inherits DbContext

    Public Contacts As DbSet(Of Contact)
    Public Companies As DbSet(Of Company)

    Protected Overrides Sub OnModelCreating(ByVal modelBuilder As System.Data.Entity.DbModelBuilder)

---Not being called




  End Sub


End Class

1 Answer 1

3

The problem is that Contacts and Companies in MVCTest1 are public member variables and not properties. Therefore they are not initialized when you address them.

Sign up to request clarification or add additional context in comments.

1 Comment

Ahhh... My bad not converting c# code I was copying properly. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.